我有 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 ?