我正在使用 aldeed:autoform 包创建表单
这就是我的代码
CompanyData = new Mongo.Collection('companyData');
CompanyData.attachSchema(new SimpleSchema({
allFields: {
type: String,
allowedValues: ['title', 'logo', 'url'],
autoform: {
type: "selectize"
}
},
title:{
type:'String',
label:'Name',
unique:true,
max:100
},
logo:{
type:'String',
label:'Logo',
optional:true
}
}));
这就是我需要的
当用户在集合中插入数据时,我想添加一个名为“createdBy”的字段,其值为 userId。
当用户更新数据时,我想添加一个名为“updatedBy”的字段,其值为 userId。
现在,当用户更新数据时,“createdBy”字段不应更新。但“updatedBy”字段应该更新。
是的,当显示表单字段时,createdBy 和 UpdatedBy 字段不会显示。
任何帮助
最佳答案
meteor-collection2 自述文件 ( https://github.com/aldeed/meteor-collection2#autovalue ) 上有明确的文档。
// Force value to be current date (on server) upon insert // and prevent updates thereafter. createdAt: { type: Date, autoValue: function() { if (this.isInsert) { return new Date; } else if (this.isUpsert) { return {$setOnInsert: new Date}; } else { this.unset(); } } }, // Force value to be current date (on server) upon update // and don't allow it to be set upon insert. updatedAt: { type: Date, autoValue: function() { if (this.isUpdate) { return new Date(); } }, denyInsert: true, optional: true }
在此文档示例中,使用了createdAt 和updatedAt。您只需更改这些以引用用户 ID。
createdBy: {
type: String,
autoValue: function() {
if (this.isInsert) {
return this.userId;
} else if (this.isUpsert) {
return {$setOnInsert: this.userId};
} else {
this.unset();
}
}
},
updatedBy: {
type: String,
autoValue: function() {
if (this.isUpdate) {
return this.userId;
}
},
denyInsert: true,
optional: true
}
您可以将其添加到每个字段以防止它显示在自动表单/快速表单中:
autoform: {
omit: true
}
或者您可以在表单中使用 omitFields="createdBy,updatedBy"
。
关于javascript - 自动表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30325369/