如何将更改事件绑定到视图中的模型以更改名称并在视图块中显示更改的名称。
Student = Backbone.Model.extend({
initialize: function(){
this.on("change:name", function(model){
var name = model.get("name");
alert("Changed my name to " + name );
});
}
});
var pe = new Student({ name: "sinduja", age: 20});
pe.set({name: 'shalini'});
最佳答案
骨干网是否不会在model.set上自动触发更改事件?
您只需要向更改事件注册视图。
Student = Backbone.Model.extend({
initialize: function(){
//initialize variables
}
});
StudentView = Backbone.View.extend({
initialize: function(model) {
_.bindAll(this);
model.on("change:name",this.nameChanged)
},
nameChanged: function(evt) {
console.log("name changed", evt)
}
})
用法:
var pe = new Student({ name: "sinduja", age: 20});
var sv = new StudentView(pe);
pe.set({name: 'shalini'});