0

我有 2 个模型作为

var Info = Backbone.Model.extend({
     defaults: {
        name: '',
        company: ''
     },
     initialize: function(){
        console.log('Object of type Info created');
     },
});

var Emp = Backbone.Model.extend({
     defaults: {
        empId: '',
        empGroup: ''
     },
     initialize: function(){
        console.log('Object of type Emp created');
     }
});

视图创建为

var model = new Info();
model.set({
    name: 'John',
    company: 'ABC'
});
model.bind('change', function(){
    model.save();
});
model.trigger('change');


var ViewClass = Backbone.View.extend({
     _modelBinder: undefined,
     initialize: function(){
         this._modelBinder = new Backbone.ModelBinder();
         this.render();
     },
     render: function(){
         var template = _.template($('#App1').html());
         this.$el.html(template);
         var bindings = {
             name: '[name=name]',
             empId: '[name=empId]'
         };
         this._modelBinder.bind(model, this.el, bindings);  // this will bind for Info. 
     }
});

HTML:

<script type="text/template" id="App1">
<div id="wrapper">
    Name: <input type="text" name="name" /><br />
    EmpId: <input type="text" name="empId" />
</div>
</script>

我们如何绑定 Info 和 Emp Models ?

4

1 回答 1

0

我真的不知道如何Backbone.ModelBinder();工作,但我想你必须创建两个绑定;

var infoBindings = {
         name: '[name=name]',
     };
     this._modelBinder.bind(infoModel, this.el, infoBindings);  // this will bind for Info.

 var empBindings = {
         empId: '[name=empId]'
     };
     this._modelBinder.bind(empModel, this.el, empBindings);  // this will bind for Emp.
于 2015-10-01T18:24:01.247 回答