您可以绑定到模型/集合上的特定更改以触发小部件的渲染方法。例如,在模型上:
var Widget1=Backbone.View.extend({
initialize: function() {
// all changes on the model will render the widget
this.model.on("change", this.render, this);
},
render: function() {
console.log("Render W1");
this.$el.html("W1 :"+this.model.get("name")+" "+this.model.get("detail"));
return this;
}
});
var Widget2=Backbone.View.extend({
initialize: function() {
// only a change on `detail` will render the widget
this.model.on("change:detail", this.render, this);
},
render: function() {
console.log("Render W2");
this.$el.html("W2 :"+this.model.get("detail"));
return this;
}
});
一个更完整的 Fiddle 可以使用http://jsfiddle.net/GqnKC/