【问题标题】:Apostrophe Widget Helpers撇号小部件助手
【发布时间】:2017-12-10 05:44:24
【问题描述】:

我必须将一些额外的数据传递给小部件,以确定我的一些用户的某些所有权/可访问性选项。我按照这里的说明设置了一个助手来传递数据:

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,这是未定义或错误的

我在这里做错了吗?

谢谢!

【问题讨论】:

    标签: apostrophe-cms


    【解决方案1】:

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

    Apostrophe 还有一个console.log 包装器,可用于{{ apos.log(somethingOfInterest) }} 之类的模板中,可用于嗅探此类事物。

    【讨论】:

      【解决方案2】:

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

      那你可以拨打{{ apos.myalias.isSubsribed() }}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多