我有以下主干视图。我有一个疑问。如果模型被删除,我会在取消后调用渲染(第一种方法),另一种方法是使用初始化函数,它使模型在视图内监听事件更改。(第二种方法)
有人可以告诉我,一和二的区别。至于两者哪个更好。
第一种方法 var AppointmentView = Backbone.View.extend({ template: _.template('">' + '<%= title %>' + 'x'),
events: { "click a": "cancel" },
cancel: function(){
this.model.cancel();
this.render(); // rendering after cancel
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});
第二种方法
var AppointmentView = Backbone.View.extend({
template: _.template('<span class="<% if(cancelled) print("cancelled") %>">' +
'<%= title %></span>' +
'<a href="#">x</a>'),
initialize: function(){
this.model.on("change", this.render, this);
},
events: { "click a": "cancel" },
cancel: function(){
this.model.cancel();
},
render: function(){
this.$el.html(this.template(this.model.toJSON()));
}
});