Javascript 如何在Meteor中添加与AutoForm的关系或引用?
我使用meteor autoform在集合中插入文档。我的Javascript 如何在Meteor中添加与AutoForm的关系或引用?,javascript,meteor,meteor-autoform,simple-schema,Javascript,Meteor,Meteor Autoform,Simple Schema,我使用meteor autoform在集合中插入文档。我的项目有一个字段groupId。提交项目表单时,如何插入此组id <template name="itemForm"> {{#autoForm type="insert" collection=Collections.Items}} {{> afQuickField name="name"}} <div class="form-group"> <button type="s
项目
有一个字段groupId
。提交项目表单时,如何插入此组id
<template name="itemForm">
{{#autoForm type="insert" collection=Collections.Items}}
{{> afQuickField name="name"}}
<div class="form-group">
<button type="submit" class="btn btn-primary">Add item</button>
<button type="reset" class="btn btn-default">Reset Form</button>
</div>
{{/autoForm}}
</template>
{{{#autoForm type=“insert”collection=Collections.Items}
{{>afQuickField name=“name”}
添加项
重置表单
{{/autoForm}
我可以创建另一个包含组id的字段,但我不希望用户看到此字段
如何设置groupId
“幕后”为此,您需要。您还需要为表单设置一个ID,比如说addItemForm
//Anywhere in your client code
Autoform.hooks({
addItemForm : {
onSubmit : function(doc) {
doc.groupId = /*Get the group id*/;
this.done(); //We've finished
return true; //Let autoForm do his default job now
}
}
});
我认为一个解决方案不是向用户显示此选项。您还需要在字段中添加
可选:true
,以便在提交表单时仍然有效
然后使用钩子,您应该能够添加您想要的任何其他数据
文件可用
我通常在插入前修改上的文档
AutoForm.hooks({
myFormId: {
before: {
insert: function(doc, template) {
//modify the document here
}
}
})
如果模板的数据上下文可用,则可以使用doc=this
例如:
<template name="itemForm">
{{#autoForm id="insert-item-form" type="insert" collection=Collections.Items doc=this}}
{{> afQuickField name="name"}}
<div class="form-group">
<button type="submit" class="btn btn-primary">Add item</button>
<button type="reset" class="btn btn-default">Reset Form</button>
</div>
{{/autoForm}}
</template>
酷。如何获取组id?现在我用this.params.\u id
在Router.route('/group/:\u id',{})中获取id代码>如果您的groupId是唯一的,则可以将其传递到全局变量中。另一个选项是在服务器上设置它。。。或者,为了提供更好的控制,可以在表单中使用方法。在字段上,可以添加autoform:{omit:true}
以在表单中隐藏字段
var itemsHooks = {
before: {
insert: function (doc) {
doc.groupId = this.currentDoc._id;
return doc;
}
}
};
AutoForm.addHooks('insert-item-form', itemsHooks);