問題是通過以下方式複製:導航視圖骨幹不刪除模型
每當我瀏覽背部模型沒有渲染。我想這是因爲此事件不會被觸發:
this.listenTo(this.collection, 'add', this.addOne);
this.listenTo(this.collection, 'reset', this.addAll);
這是我的路由器:
routes: {
'' : 'home',
'home' : 'home',
'departments' : 'departments',
...
},
home: function(){
var view = new app.HomeView();
this.showView(view);
},
departments: function(){
var view = new app.DepartmentsView();
this.showView(view);
},
showView: function(view){
if (this.currentView){
this.currentView.clean();
}
this.currentView = view;
this.currentView.render()
$('#container').html(this.currentView.el);
}
這是我的清潔方法:
Backbone.View.prototype.clean = function() {
this.remove();
this.unbind();
_.each(this.subViews, function(subView){
subView.clean();
if(subView.onClose){ subView.onClose() }
});
};
這是的OnClose方法在子視圖上:
onClose: function(){
this.model.off('change',this.render);
this.model.off('destroy',this.remove);
console.log('unbinding');
}
我將所有子視圖保存在數組上,然後在導航時關閉。我真的沒有發現這個問題的根源。
我很絕望,因爲我已經嘗試了Derick Bailey的所有帖子,並通過Backbone的文檔進行了嘗試,但無法解決此問題。
------ -------- EDIT 視圖組成爲:
- 父視圖:包含一個表,首標,沒有行。
- 子視圖:哪些是行 - >
<tr>
- 模式的看法:這是一個包含用於創建模型
這是我的回購,如果你想看看形式 視圖。實際上重現最小的事情是在一個問題中發佈很多代碼。 REPO
我很感謝幫助。
當你瀏覽別的車型應該被刪除的權利..?他們爲什麼還在那裏..?他們在哪裏創造..?誰都參考他們..? –
我編輯了這個問題。如果你希望你可以在app/scripts文件下看到repo。對於此示例,請檢查departmentsView - >父視圖,departmentView - >行,departmentModalView - > Modal。路由器在main.js下的路由器文件夾和我乾淨的方法下。 –
迭戈。當您第二次導航到部門頁面時,您是否可以確認模型是否在集合中? –