【问题标题】:Apostrophe-cms cloning a pieces area into another non pieces areaApostrophe-cms 将一个碎片区域克隆到另一个非碎片区域
【发布时间】:2021-03-11 07:06:36
【问题描述】:

我有一个名为模板的 apos 片段集合,在该架构中我有一个用于其他小部件等的 apos 区域。

我已经创建了一个标准的非碎片小部件,它带有一个可以选择区域的 joinbyone,但我似乎找不到将碎片区域中的项目克隆到小部件中的非碎片区域的方法。

我的想法是我可以将模板创建为片段,当它们被选中时,我会将该模板的副本创建到一个匿名区域中,这样片段区域就不会改变。任何帮助将不胜感激

【问题讨论】:

    标签: apostrophe-cms apostrophe


    【解决方案1】:

    小部件没有自己的afterSave 类型event handler,因为实际上并不是它们被保存。这是他们所属的文件。您可以在 doc 类型上使用 afterSave 事件处理程序来:

    1. 查看保存的文档是否在某个区域中包含该小部件
    2. 查看模板小部件是否有填充连接
    3. 将连接文档中的区域数据复制到小部件区域
    4. 清除小部件的连接字段(以便它可以查看将来是否选择了新模板)

    步骤 3b 实际上是更新复制区域中所有小部件的_id 属性。区域小部件在移动或复制时具有唯一的_ids,因此应该为这个新上下文新生成这些小部件。 cuid 实用程序对此很有用。

    这是一个不寻常的案例,但它确实很有趣。如果你需要这样做,我认为这可能会起作用。它使数据保持正确,而不是在模板中进行数据工作。

    【讨论】:

    • 值得了解使用它的上下文 - 构建模板以包含许多东西,例如小部件和其他递归模板。指向这些模板的 ref 链接需要保持完整,以便文档可以维护在构建该文档时使用的所有模板的列表(但它不再需要这些模板)。所以我想要一个与新的合并标签不同但可变的系统。从用户体验的角度来看,我的方法流程按预期工作 - 我认为你的建议会留下一个空白区域,直到保存开始,这不会是好的用户体验。
    【解决方案2】:

    我似乎无法在数据服务器端获得句柄,因此我使用模板帮助程序将数据重新发布,然后从片段中复制该区域并通过重新点路径/docid 运行项目常规。到目前为止效果很好!

    模板.html

     {% import "macros/widgets.html" as widget %}
    {%if data.widget%}
    
    {% set section=apos.sectionTemplate.copyme(data.widget) %}
    
    {%endif%}
    
    {{ widget.postWidget(section, 'newnsection') }}
    

    widget.js

    const apos = require("apostrophe");
    
    module.exports = {
      extend: 'apostrophe-widgets',
      label: 'Section Template',
      alias: 'sectionTemplate',
      defer: true,
      addFields: [
        {
          name: '_nsection',
          type: 'joinByOne',
          withType: 'templateSections',
          label: 'Template',
          required: true,
          idField: 'id',
          filters: {
            projection: {
              title: 1,
              nsection: 1
            }
          }
        }
        ,
        {
          name: 'newnsection',
          type: 'area',
          label: 'Section',
          contextual: true,
          def: ''
        }
      ],
    
      construct: function (self, options) {
    
        self.addHelpers({
          copyme: function (data) {
            if (data.newnsection.items.length == 0) {
              data.newnsection.items = data._nsection.nsection.items
              area = recalcDotPathsAreas(data.newnsection);
    
              function recalcDotPathsAreas(area) {
                var docId = area._docId;
                var dotPath = area._dotPath;
                area.items.forEach(stlevel);
                function stlevel(value, index, array) {
                  var widgetDotPath = dotPath + '.items.' + index;
                  try {
                    var $widget = value;
                    value.__docId = docId;
                    value.__dotPath = widgetDotPath;
                    recalcDotPathsAreasWidget(value, docId, widgetDotPath);
                  } catch (e) {
                  }
                };
                return area
              };
    
              function recalcDotPathsAreasWidget($widget, docId, dotPath) {
                $widget.items.forEach(sclevel);
                function sclevel(value, index, array) {
                  var $area = value;
                  var areaDocId = value._id;
                  if ((areaDocId !== docId) && areaDocId && (areaDocId.substring(0, 1) === 'w')) {
                    value._id = docId;
                    areaDocId = docId;
                  }
                  if (areaDocId === docId) {
                    var areaDotPath = value._dotPath;
                    if (areaDotPath) {
                      var components = areaDotPath.split('.');
                      var name = components.pop();
                      value._dotPath = dotPath + '.' + name;
                      recalculateDotPathsInArea(value);
                    }
                  }
                };
              };
            }
            return data;
          }
        });
    
        var superPushAssets = self.pushAssets
        self.pushAssets = function () {
          superPushAssets()
          //  self.pushAsset('stylesheet', 'smart-section', {when: 'always'})
          self.pushAsset('script', 'always', { when: 'always' })
        }
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 1970-01-01
      • 2021-09-27
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      相关资源
      最近更新 更多