撇号之前保存不起作用

时间:2018-05-27 14:43:13

标签: apostrophe-cms

这是自动更新标题的架构和构造方法。但它根本不起作用!它一直提示我填写全名。您可以将我的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();
        };
    }
};

2 个答案:

答案 0 :(得分:2)

谢谢Stuart Romanek,现在我知道必填字段会提示用户填写。我可以像你说的那样使用上下文覆盖它。问题是, slug 。但我认为我也必须加上上下文

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();
        }
    }
};

答案 1 :(得分:1)

用户提交片段后,

beforeSave将在服务器上发生,因此浏览器端required验证将在有机构构{{1}之前停止提交属性。

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