如何访问apos.define内部的选项?

时间:2018-06-25 19:12:40

标签: javascript node.js apostrophe-cms

select具有以下内容:

public / js / user.js

apostrophe-workflow

我搜索了很长时间,但没有设法找到此构造方法可以访问apos.define('apostrophe-workflow', { [...] construct: function(self, options) { self.locales = options.locales; self.locale = options.locale; [...] 对象的原因。我尝试过options,但不确定如何正确使用它。

我的资产也使用browserCall推送。但是他们无权访问pushAsset之后的选项。

编辑:示例场景:

一个简单的模块,将一个脚本推送到浏览器。

module / index.js

apos.create

并采取一种选择。

app.js

construct: function(self, options) {
  self.pushAsset('script', 'name', {when: 'always'});
}

脚本应在modules: { 'module': { option: 'Option' } } 上使用此选项。

module / public / js / script.js

construct

另一个模块将调用apos.define('module-script', { construct: function(self, options) { console.log(options.option); // Print 'Option' to console. } });

我希望这很清楚。

1 个答案:

答案 0 :(得分:1)

根据所需的结构,您可以(至少)解决这两种方法。

1。显式浏览器选项

您可以通过将选项包装在模块配置根目录的browser对象中,将选项从模块配置显式传递给浏览器。

lib/modules/layout-widgets/index.js

module.exports = {
    extend: 'apostrophe-widgets',
    label: 'Layout',
    browser: {
      coolArray: [3, 2, 1]
    }
  }

这将自动合并到传递给模块的浏览器侧​​JS的选项中。

然后在/lib/modules/layout-widgets/public/js/always.js

apos.define('layout-widgets', {
  extend: 'apostrophe-widgets',
  construct: function (self, options) {
    self.play = function ($widget, data, options) {
      console.log(self.options.coolArray);
    }
  }
});

2。超级getCreateSingletonOptions

如果您不喜欢将浏览器选项与主选项分开的语法,则始终可以通过复制,调用并添加该方法来覆盖负责准备浏览器侧模块默认选项的方法。

lib/modules/layout-widgets/index.js

module.exports = {        
  extend: 'apostrophe-widgets',        
  label: 'Layout',
  coolArray: [3,2,1],
  construct: function(self, options) {

    // copy the method
    var superGetCreateSingletonOptions = self.getCreateSingletonOptions;

    // redefine it
    self.getCreateSingletonOptions = function (req) {

      // invoke the original method and save the result
      var browserOptions = superGetCreateSingletonOptions(req);

      // add on to the default results with whatever you want
      browserOptions.coolArray = self.options.coolArray;
      browserOptions.somethingElse = 'hey this is fun';

      return browserOptions;
    };
  }
};

然后再次在/lib/modules/layout-widgets/public/js/always.js

apos.define('layout-widgets', {
  extend: 'apostrophe-widgets',
  construct: function (self, options) {
    self.play = function ($widget, data, options) {
      console.log(self.options.coolArray);
      console.log(self.options.somethingElse);
    }
  }
});