Search code examples
apostrophe-cms

Apostrophe Widget Helpers


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!


Solution

  • 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.