我正在将 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 Hook 以及 Meteors 内置模板“提交”事件处理程序。如果我使用 jQuery onsubmit 事件处理程序,它会注册该事件。我不能为此目的使用 jQuery,因为 autoform 必须验证输入。
最佳答案
由于 created
和 modified
是必填字段,因此很可能不会提交,因为表单中缺少这些字段,这意味着表单无效。实际上有几种不同的方法可以解决这个问题:
- 将它们设为可选(可能不是您想要的)
- 为自动表单的
schema
属性使用不同的模式,一个没有这两个字段的模式。 - 添加一个“方法前” Hook ,在提交的文档中设置这两个字段。
- 使用
autoValue
为这两个字段生成值。
因为使用 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;
}
}
}
此外,为了帮助在开发过程中更轻松地找出此类问题,我建议启用 debug mode .
关于javascript - 带有 collection2 的 Meteor Autoform 包不提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23156715/