更新: 根據我的評論,我的問題是,我有一個額外的模型,我傳遞到視圖中,我並未解除綁定事件。當我看到事件處理程序被觸發時,我認爲源是來自this.model而不是this.extra_model,因爲我忘記了這個.extra_model也被用於錯誤驗證。殭屍事件仍然在移除骨幹視圖後
解決的辦法是添加以下內容:
MyView = Backbone.extend({
//...
//add method to override BaseView
cleanUp: function() {
this.extra_model.off(null, null, this);
BaseView.prototype.cleanUp.apply(this, arguments);
},
//...
});
感謝審查的問題,併爲程序員錯誤抱歉。
所有: 我在與我已經清理了一個視圖後,仍然被束縛陳舊/殭屍事件的問題。當我將自定義事件綁定到模型時,問題就出現了。當我從dom中刪除視圖時,我調用'this.model.off(null,null,this);'正如在各種消息板上所建議的,但儘管我可以在chrome調試器工具中看到「自定義處理程序」回調被刪除,但我仍然注意到「自定義處理程序」的事件處理程序被調用的次數比它應該多(每個額外的一次當我清理它時重新創建視圖)觸發事件時。有人能告訴我,如果我的清理代碼丟失了什麼?提前致謝!
BaseView = Backbone.extend({
//...
displayErrors:function(){},
cleanUp: function(){
if (this.model) this.model.off(null, null, this);
if (this.collection) this.collection.off(null, null, this);
if (!this.options.persistDataAfterViewCleanup) {
if (this.model) delete this.model;
if (this.collection) delete this.collection;
}
//_.each(this.subViews, function(view){view.cleanUp();}); not needed yet.
this.undelegateEvents();
$(this.el).removeData().unbind();
//Remove view from DOM
this.$el.html('');
this.remove();
}
});
MyView = BaseView.extend({
initialize: function(){
//called manually from model using trigger
this.model.on('custom-handler', this.displayErrors, this);
}
});
原來,這是我自己的編程錯誤導致的問題。我有一個額外的模型,我使用的觀點是我必須堅持不同的路線,並且在調用清理之前,我並沒有解除綁定該模型的事件。我通過在子視圖中重寫cleanUp來解決該問題,解除綁定該額外模型的所有事件,然後調用BaseView.prototype.cleanUp.apply(this,arguments); 對不起,我不知道你能做到這一點 – Daniel 2013-03-19 22:07:21