【问题标题】:apostrophecms beforeSave won't work撇号 beforeSave 不起作用
【发布时间】:2018-05-28 15:41:32
【问题描述】:

这是我自动更新标题的架构和构造方法。但它根本不起作用!它一直提示我填写 Full Name 。你可以 fork 我的 github 来查看整个代码 My Github Apostrophe Tutorial 。有人请帮助我。我深深地爱上了撇号。我一直关注的教程是Setting the Title Automatically

module.exports = {
    extend: 'apostrophe-pieces',
    permissionsFields : true,
    name: 'person',
    label: 'Person',
    pluralLabel: 'People',
    beforeConstruct : function(self,options){
        options.addFields= 
        [
            {
                name: 'title',
                label: 'Full Name',
                type: 'string',
                required: true
            },
            {
                name: 'firstName',
                label: 'First Name',
                type: 'string',
                required: true
            },
            {
                name: 'lastName',
                label: 'Last Name',
                type: 'string',
                required: true
            },
            {
                name: 'body',
                label: 'Biography',
                type: 'area',
                options: {
                    widgets: {
                        'apostrophe-rich-text': {
                            toolbar: ['Bold', 'Italic', 'Link', 'Unlink']
                        },
                        'apostrophe-images': {}
                    }
                }
            },
            {
                name: 'phone',
                label: 'Phone',
                type: 'string'
            },
            {
                name: 'thumbnail',
                label: 'Thumbnail',
                type: 'singleton',
                widgetType: 'apostrophe-images',
                options: {
                    limit: 1,
                    minSize: [200, 200],
                    aspectRatio: [1, 1]
                }
            }
        ].concat(options.addFields || [])
    },
    arrangeFields: [{
            name: 'contact',
            label: 'Contact',
            fields: ['firstName', 'lastName', 'phone']
        },
        {
            name: 'admin',
            label: 'Administrative',
            fields: ['slug', 'published', 'tags']
        },
        {
            name: 'content',
            label: 'Biographical',
            fields: ['thumbnail', 'body']
        }
    ],
    construct: function(self, options) {
        self.beforeSave = function(req, piece, options, callback) {
            piece.title = piece.firstName + ' ' + piece.lastName;
            return callback();
        };
    }
};

【问题讨论】:

    标签: apostrophe-cms


    【解决方案1】:

    beforeSave 将在用户提交作品后发生在服务器上,因此浏览器端的required 验证将在它有机会构造title 属性之前停止提交。

    您可以省略title 字段上的required 属性,您的beforeSave 将按预期工作。如果您想强制以编程方式设置标题并且不在表单中包含该字段,您可以在title 字段上设置contextual: true,该字段将不会在管理器中呈现。

    【讨论】:

      【解决方案2】:

      谢谢 Stuart Romanek,现在我知道必填字段会提示用户填写。我可以像你说的那样使用 contextual 覆盖它。问题是,slug。但我想我也必须把 contextual 放在一起。

      module.exports = {
          extend: 'apostrophe-pieces',
          permissionsFields : true,
          name: 'person',
          label: 'Person',
          pluralLabel: 'People',
          beforeConstruct : function(self,options){
              options.addFields= 
              [
                  {
                      name: 'firstName',
                      label: 'First Name',
                      type: 'string',
                      required: true,
                  },
                  {
                      name: 'lastName',
                      label: 'Last Name',
                      type: 'string',
                      required: true
                  },
                  {
                      name: 'title',
                      label: 'Full Name',
                      type: 'string',
                      required: true,
                      contextual : true
                  },
                  {
                      name: 'slug',
                      label: 'Slug',
                      type: 'string',
                      required: true,
                      contextual: true
                  },
                  {
                      name: 'body',
                      label: 'Biography',
                      type: 'area',
                      options: {
                          widgets: {
                              'apostrophe-rich-text': {
                                  toolbar: ['Bold', 'Italic', 'Link', 'Unlink']
                              },
                              'apostrophe-images': {}
                          }
                      }
                  },
                  {
                      name: 'phone',
                      label: 'Phone',
                      type: 'string'
                  },
                  {
                      name: 'thumbnail',
                      label: 'Thumbnail',
                      type: 'singleton',
                      widgetType: 'apostrophe-images',
                      options: {
                          limit: 1,
                          minSize: [200, 200],
                          aspectRatio: [1, 1]
                      }
                  }
              ].concat(options.addFields || [])
          },
          arrangeFields: [{
                  name: 'contact',
                  label: 'Contact',
                  fields: ['firstName', 'lastName', 'phone']
              },
              {
                  name: 'admin',
                  label: 'Administrative',
                  fields: ['slug', 'published', 'tags']
              },
              {
                  name: 'content',
                  label: 'Biographical',
                  fields: ['thumbnail', 'body']
              }
          ],
          construct: function(self, options) {
              self.beforeSave = function(req, piece, options, callback) {
                  // Override title and MUST SET CONTEXTUAL to able to save. Let the 
                  // backend self.beforeSave method do this thing.
                  // You know why I don't set piece.slug ?
                  // Because once you already set title , apostrophe made it for you :)
                  // BUT must put contextual : true on slug. If not, it will prompt you :*
                  piece.title = piece.firstName + ' ' + piece.lastName;
                  return callback();
              }
          }
      };

      【讨论】:

        猜你喜欢
        • 2015-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多