撇号小部件助手

Apostrophe Widget Helpers

我必须将一些额外的数据向下传递到小部件以确定我的某些用户的某些 ownership/accessibility 选项。我按照此处的说明设置了一个助手来传递数据:

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

不过,我 运行 遇到了一些问题。下面的代码用于小部件 index.js 文件:

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
    }
  ]
};

我没有 运行 遇到任何错误,但是当我尝试从小部件的模板实际访问帮助程序时,它找不到方法。我尝试了以下方法来访问它并尝试取回值:

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

但它们都给我以下错误之一:

错误:无法调用未定义或错误的 apos["isSubscribed"] 错误:无法调用未定义或错误的 clap

我是不是做错了什么?

谢谢!

在您的模板中,您应该可以调用 apos.modules['my-module-widgets'].isSubscribed

Apostrophe 也有一个 console.log 包装器,可用于 {{ apos.log(somethingOfInterest) }} 等模板,可用于嗅探此类内容。

您还可以在 module.exports 中提供别名。 alias: 'myalias'

那你可以打电话给{{ apos.myalias.isSubsribed() }}