我正在将 collection2 和 autoform 与一级嵌套架构一起使用
Node = new Meteor.Collection('node',{
schema: new SimpleSchema({
content: {
type: String,
label: "Content",
max: 200
}
created: {
type: Date,
label: "Created Date"
},
modified: {
type: Date,
label: "Modified Date"
}
})
});
NodeMeta = new Meteor.Collection('Node_meta',{
schema: new SimpleSchema({
desc:{
type:String,
max:200,
label:"Description",
optional:true
}
})
});
Schema = {};
Schema.nodeWithMeta= new SimpleSchema({
Node: {
type: Node.simpleSchema()
},
Meta: {
type: NodeMeta.simpleSchema()
}
});
{{#autoForm schema=schema id="nodeForm" type="method" meteormethod="nodecreate"}}
{{> afFieldInput name='Node.content' rows=8 }}
{{> afFieldInput name='Meta.desc' rows=8}}
<button type="submit" class="btn btn-primary btn-submit">Create</button>
{{/autoForm}}
Template.nodeCreate.helpers({
schema: function() {
return Schema.nodeWithMeta;
}
});
它不调用服务器方法。我尝试了 autoform onSubmit 挂钩以及 Meteors 内置模板“提交”事件处理程序。如果我使用 jQuery onsubmit 事件处理程序,它会注册该事件。我不能使用 jQuery 来实现此目的,因为自动表单必须验证输入。
Since created
and modified
是必填字段,很可能不会提交,因为表单中缺少这些字段,这意味着表单无效。实际上有几种不同的方法可以解决这个问题:
- 将它们设为可选(可能不是您想要的)
- 使用不同的架构,其中一个没有这两个字段,
schema
自动表单的属性。
- 添加一个“before method”挂钩,用于在提交的文档中设置这两个字段。
- Use
autoValue
生成这两个字段的值。
由于创建和修改日期相当容易处理自动值 https://github.com/aldeed/meteor-simple-schema#autovalue,我会这样做。像这样的事情:
created: {
type: Date,
label: "Created Date",
autoValue: function () {
if (this.isInsert) {
return new Date;
} else {
this.unset();
}
}
},
modified: {
type: Date,
label: "Modified Date",
autoValue: function () {
if (this.isInsert) {
this.unset();
} else {
return new Date;
}
}
}
另外,为了帮助在开发时更轻松地解决此类问题,我建议启用调试模式 https://github.com/aldeed/meteor-simple-schema#debug-mode.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)