【发布时间】:2014-10-11 23:44:49
【问题描述】:
我有这个 Meteor 模板:
<template name="personalDetailsForm">
{{> quickForm collection="PersonalDetails" id="personalDetailsForm" type="insert"}}
{{> quickForm collection="PersonalDetails" doc=editingDoc id="personalDetailsForm" type="update"}}
</template>
表单按我的预期显示,但我只想要一个表单。提交表单时没有插入数据的空白表单。然后,当重新加载表单时,先前提交的数据会显示在表单上。如果再次提交表单,任何已更改的数据都将被更新。
目前正在显示插入表单,在其下方显示更新表单,其中包含先前插入的数据。尝试更新第二个表单上的数据不起作用,而是插入一条新记录。我想这可能是因为表单 ID 相同。
理想情况下,我想做这样的事情:
<body>
{{#if PersonalDetails}}
{{> personalDetailsFormUpdate}}
{{ else }}
{{> personalDetailsFormInsert}}
{{/if}}
</body>
<template name="personalDetailsFormInsert">
{{> quickForm collection="PersonalDetails" id="personalDetailsFormInsert" type="insert"}}
</template>
<template name="personalDetailsFormUpdate">
{{> quickForm collection="PersonalDetails" doc=editingDoc id="personalDetailsFormUpdate" type="update"}}
</template>
我认为这部分文档是我正在寻找的:
我可以为插入和更新重复使用相同的 quickForm 或 autoForm 吗?
是的。您在状态之间翻转的代码应按此顺序执行以下操作:
Change the type attribute's value to "insert" or "update" as appropriate, probably by updating a reactive variable. Change the doc attribute's value to the correct document for an update or to null (or a document containing default values) for an insert, probably by updating a reactive variable. Call AutoForm.resetForm(formId). This will clear any existing validation errors for the form.
谁能举个例子?
【问题讨论】:
标签: javascript meteor