apostrophe-cms - 撇号小部件助手

标签 apostrophe-cms

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

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) }} 这样的模板中,对于嗅探这种类型的

关于apostrophe-cms - 撇号小部件助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722204/

相关文章:

css - 通过管理 UI 显示 flex 小部件样式干扰

javascript - 无法在小部件撇号中保存图像

javascript - apostrope-images-widgets 自定义小部件调用 apos.attachments.url 两次

javascript - 使用 express 中间件设置 cookie

javascript - 在 Apostrope CMS 项目中包含外部第 3 方脚本

apostrophe-cms - 将后端数据注入(inject) View 以在客户端 JS 中使用

npm - 永远安装 npm 和 mechanic 时出错

node.js - 停止回调链并在 Save 方法 ApostropeCMS 之前发送通知

apostrophe-cms - 如何将js脚本传递给特定的页面类型?

javascript - 在 ApostropeCMS 中编辑时添加只读字段