2015-10-01 101 views
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> 

我們如何可以綁定兩個信息和僱員模型?

回答

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.