之前的撇号保存不起作用



这是我自动更新标题的架构和构造方法。但它根本行不通!它一直提示我填写全名。你可以分叉我的github以查看整个代码My Github撇号教程。有人请帮帮我。我非常爱上撇号。我遵循的教程是自动设置标题

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

beforeSave

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

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

谢谢斯图尔特·罗曼内克,现在我知道必填字段会提示用户填写。我可以像你说的那样使用上下文来覆盖它。问题是,蛞蝓。但我想我也必须加上上下文

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

最新更新