0

我正在 MeteorJS 上编写 CRUD 应用程序。我有很多不同输入的 html 表单。在更新情况下,我无法为来自 mongodb 的 html 输入设置默认值。如何在创建和更新案例中使用相同的 html 表单?

谢谢。

4

1 回答 1

0

您可以在模板中使用一个表单{{>template}}并使用另一个表单,以便您可以分离事件绑定(如果您使用流星,否则您可以{{>form}}使用 JQuery

客户端 HTML

<template name="form">
    <form>
        <input type="text" value="{{values.fieldname1}}"/>
    </form>
</template>

<template name="crud">
   <h1>Update</h1>
   {{>update}}
   <hr/>
   <h1>New</h1>
   {{>create}}
</template>

<template name="update">
 {{>form}}
</template>

<template name="create">
 {{>form}}
</template>

客户端JS

Template.update.values = function() {
    return MyCollection.findOne()
}

Template.update.events({
    'submit':function(event,context) {
        //update your stuff (your data would be in context.data)
        event.preventDefault()
    }
});

Template.create.events({
    'submit':function(event,context) {
        //create your new item
        event.preventDefault();
    }
});
于 2013-03-17T20:05:52.950 回答