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.
}
});
。
我希望这很清楚。
答案 0 :(得分: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);
}
}
});
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);
}
}
});