Apostrophe Widget Helpers

梦想的初衷 提交于 2019-12-10 11:25:23

问题


I have to pass some extra data down to a widget to determine certain ownership/accessibility options of some of my users. I was following the instructions here in order to set up a helper to pass the data:

http://apostrophecms.org/docs/technical-overviews/how-apostrophe-handles-requests.html#template-helpers-invoking-synchronous-java-script-code-from-your-template

I have run into some issues, however. The code below is for the widget index.js file:

module.exports = {
  extend: 'apostrophe-widgets',
  label: 'Unsubscribed Content',
  //contextualOnly: true,
  construct: function(self, options) {

    self.addHelpers({
      isSubscribed: function() {

        var req = self.apos.templates.contextReq;
        var isSubbed = false;
        var currentYear = new Date().getFullYear();
        for(var i = 0; i < req.user.subscriptions.length; i++) {
          if (req.user.subscription[i].subscriptionYear == currentYear) {
            isSubbed = true;
          }
        }

        return isSubbed;
      }
    });

  },
  addFields: [
    {
      type: 'area',
      name: 'area',
      label: 'Area',
      contextual: true
    }
  ]
};

I don't run into any errors here, but when I try to actually access the helper from the widget's template, it cannot find the method. I've tried the following ways to access it and just try to get the value back:

{{ apos.isSubscribed() }}
{{ isSubscribed() }}
{% if apos.isSusbcribed() %}
{% if isSubscribed() %}

But all of them give me one of the following errors:

Error: Unable to call apos["isSubscribed"], which is undefined or falsey Error: Unable to call clap, which is undefined or falsey

Am I doing something wrong here?

Thanks!


回答1:


In your template you should be able to call apos.modules['my-module-widgets'].isSubscribed

Apostrophe also has a console.log wrapper which can be used in templates like {{ apos.log(somethingOfInterest) }}, useful for sniffing around this type of thing.




回答2:


You can also provide an alias in your module.exports. alias: 'myalias'

Then you can call {{ apos.myalias.isSubsribed() }}



来源:https://stackoverflow.com/questions/46722204/apostrophe-widget-helpers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!