【问题标题】:Extend widgets for common input fields in apostrophe cms扩展撇号 cms 中常见输入字段的小部件
【发布时间】:2018-06-06 19:44:04
【问题描述】:

我在撇号 2.x 中创建了多个工作正常的小部件。现在我想正确地组织和组织它们。 我的大多数模块都有从内容宽度更改为浏览器宽度的选项。我试图概括这个函数,而不把代码放在每个模块中。

例如在我的 layout-widget/index.js 我有这个:

module.exports = {
  extend: 'apostrophe-widgets',
  label: 'Layout',
  addFields: [{
    name: 'size',
    label: 'Modulbreite',
    type: 'select',
    choices: [{
        label: 'Content width',
        value: 'content-width'
      },
      {
        label: 'Full width',
        value: 'full-width'
      }
    ],
    required: true
  }]
};

现在我想通过扩展这个通用模块配置来创建其他小部件,以获得单独的配置选项以及此选择。 文件:test-widget/index.js

module.exports = {
  extend: 'layout-widgets',
  label: 'Test',
  addFields: [{
      name: 'headline',
      label: 'Headline',
      type: 'string',
      required: true
    },
    {
      name: 'content',
      label: 'Add Element',
      type: 'area',
      options: {
        widgets: {
          'fact': {},
          'text': {}
        }
      }
    }
  ]
};

但目前我在我的 CMS 中没有看到测试小部件的大小选项。我只看到直接在测试模块中定义的选项。这甚至可能还是我必须将尺寸选择插入每个模块本身?

最好的问候

【问题讨论】:

    标签: apostrophe-cms


    【解决方案1】:

    这是可能的,你只需要记住这个意图来编写'layout-widgets/index.js'。该模块不能只设置 addFields ,因为它将被扩展它的模块覆盖。相反,它必须使用beforeConstruct 函数来操作addFields。以下是来自apostrophe-pieces 模块的示例:

    beforeConstruct: function(self, options) {
      options.addFields = [
        {
          type: 'boolean',
          name: 'published',
          label: 'Published',
          def: false
        }
      ].concat(options.addFields || []);
    }
    

    beforeConstruct 应与construct 处于同一级别,即不嵌套在其中。

    【讨论】:

    • 哦,太棒了!感谢您的解释。 @汤姆
    • 这就像一个魅力。刚刚测试过了。显着减少了我的 index.js 文件的长度 :)。辛苦了,谢谢
    猜你喜欢
    • 2022-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2021-03-30
    • 1970-01-01
    相关资源
    最近更新 更多