How to access options inside apos.define?

孤者浪人 提交于 2019-12-11 07:04:05

问题


apostrophe-workflow has the following:

public/js/user.js

apos.define('apostrophe-workflow', {

  [...]

  construct: function(self, options) {

    self.locales = options.locales;
    self.locale = options.locale;
    [...]

I searched quite a while and did not manage to find the reason why this construct method has access to the options object. I tried browserCall but am not sure how to use this properly.

My assets are pushed using pushAsset, too. But they do not have access to the options after apos.create.

Edit: Example scenario:

A simple module that pushes one script to the browser.

module/index.js

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

And takes one option.

app.js

modules: {
  'module': {
    option: 'Option'
  }
}

The script should use this option on construct.

module/public/js/script.js

apos.define('module-script', {
  construct: function(self, options) {
    console.log(options.option); // Print 'Option' to console.
  }
});

Another module will call apos.create('module-script').

I hope it's clear.


回答1:


You can solve (at least) this two ways depending on the structure you want.

1. Explicit browser options

You can explicitly pass options to the browser from your modules configuration by wrapping them in a browser object from the root of the module's config.

in lib/modules/layout-widgets/index.js

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

This will get merged into the options passed to your browser side JS of the module automatically.

then in /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. Super'ing getCreateSingletonOptions

If you don't like the syntax of separating your browser options from your main options, you can always override the method responsible for teeing up the browser side module's default options by copying it, invoking it, and adding on to it.

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

then, again, in /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);
    }
  }
});


来源:https://stackoverflow.com/questions/51030428/how-to-access-options-inside-apos-define

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!