Backbone.js 使用木偶和LoDash填充输入值

Backbone.js 使用木偶和LoDash填充输入值,backbone.js,marionette,backbone-views,lodash,Backbone.js,Marionette,Backbone Views,Lodash,我的目标是在模型更改时填充木偶模板 我有一个模板,它是一个表单: <!-- EditContact.html --> <fieldset> <label for="name">Name</label> <input type="text" id="name" name="name" maxLength="64" value="{{name}}"> </fieldset> <fieldset>

我的目标是在模型更改时填充木偶模板

我有一个模板,它是一个表单:

<!-- EditContact.html -->
<fieldset>
    <label for="name">Name</label>
    <input type="text" id="name" name="name" maxLength="64" value="{{name}}">
</fieldset>

<fieldset>
    <label for="email">Email</label>
    <input type="text" id="email" name="email" value="{{email}}" />
</fieldset>

我使用相同的布局创建记录。如何更新视图以使用模型自动填充字段。我是否必须为每个元素手动使用jquery,或者我可以在视图中使用lodash进行此操作?

看起来您正在寻找编辑联系人操作示例。下面是一个很好的例子

var ContactController = Backbone.Marionette.Controller.extend({

    editAction: function(contactId) {

        ContactCollection = Backbone.Collection.extend({
            url: "/contacts"
        });
        var contactCollection = new ContactCollection();

        contactCollection.fetch().done(function(){
            // executed after the contacts from server are retrieved

            // get the specific contactId
            var contact = contactCollection.get(contactId);

            // show model in the edit view
            var editView = new ContactEditView({model: contact});
            applayout.show(editView);
        });

    }

});
木偶视图具有属性“modelEvents”e“collectionEvents”。应该尽可能多地使用它们,因为它们使代码更干净


查看关于它们的文档:。

是否要在模型更改事件中呈现模板?
var ContactController = Backbone.Marionette.Controller.extend({

    editAction: function(contactId) {

        ContactCollection = Backbone.Collection.extend({
            url: "/contacts"
        });
        var contactCollection = new ContactCollection();

        contactCollection.fetch().done(function(){
            // executed after the contacts from server are retrieved

            // get the specific contactId
            var contact = contactCollection.get(contactId);

            // show model in the edit view
            var editView = new ContactEditView({model: contact});
            applayout.show(editView);
        });

    }

});