如何在 Meteor 中添加与自动表单的关系或引用
How to add a relationship or reference with AutoForm in Meteor?
我使用 meteor-autoform
在集合中插入文档。我的Items
有一个现场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>
我可以创建另一个包含我的组 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
}
}
});
我认为一种解决方案不是向用户显示此选项。您还需要向该字段添加optional:true
,以便在您提交表单时它仍然有效。
然后使用钩子,您应该能够添加所需的任何其他数据
文档在自动表单上可用钩子
我通常会修改before insert
上的文档
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>
进一步的结果是,您可以设置一个将在插入操作之前触发的钩子:
var itemsHooks = {
before: {
insert: function (doc) {
doc.groupId = this.currentDoc._id;
return doc;
}
}
};
AutoForm.addHooks('insert-item-form', itemsHooks);
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何使用WCF服务和javascript表单post上传.doc文件
- Javascript生成的表单未提交
- 如何使用javascript或html下载PDF格式的填写表单
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- Ajax发布表单序列化,发布引号'
- 多级HTML表单
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 在验证和发送邮件后更改联系人表单的 html
- jQuery表单添加不适用于下拉列表
- Rails/JSON:如何将JSON用于jquery UI自动完成表单
- HTML 表单文件中的行号与文本区域中的行号之间的对应关系
- 如何在 Meteor 中添加与自动表单的关系或引用
- 如何将 IF 语句应用于表单的<选择>关系
- 每页具有多个表单的输入元素之间的依赖关系
- 当涉及下拉依赖关系时,javascript中的表单验证
- Dynamics CRM根据父表单/实体在新表单/实体中填充关系数据
- 处理基于矩阵的表单元素关系