最近在用backbone开发项目时,遇到在model的change事件被重复绑定的问题。我在来回切换几次view(页面)的时候,change就会被重复绑定几次。
代码如下:
var ItemView = Backbone.View.extend({
tagName: 'div',
className:'acc-item',
initialize:function(opts){
this.template = opts.mainView.itemTpl;
this.dayView = opts.dayView;
this.listenTo(this.model, 'destroy', this.remove);
this.listenTo(this.model,'change',this.render);
},
render:function(){
return this;
},
remove:function(){
}
});
请教一下,该怎么解决
解决问题了,在initialize里面写this.model.off('change')和this.model.off('destroy')即可!