我使用一個自定義的基本視角,延伸骨幹remove方法:
app.BaseView = Backbone.View.extend({
views: [], // array to keep a ref of child-views for proper disposal later on
remove: function() {
// dispose any sub-views
_.each(this.views || [], function(view) {
view.remove();
});
// if the inheriting class defines a custom on-remove method, call it!
_.isFunction(this.onRemove) && this.onRemove();
// unbind all events from this view
this.off();
// finally, call Backbone's default remove method to
// remove the view from the DOM
Backbone.View.prototype.remove.call(this);
}
}
仍然有陷阱:模型和收藏需要手工配置,因爲你如果它使用的不知道做其他觀點也一樣。
不錯的文章。感謝您的鏈接。 – ProTom
是的文章是朝着正確的方向發展的,但畢竟視圖是負責清理的。我鎖定一個解決方案,視圖不關心清理,導致別人正在做這件事。考慮有時候有人忘記手動清理的大型團隊。你也必須一遍又一遍地寫相同的代碼。 –
這樣的系統並不存在於主幹中,因爲他們想要提供一個結構並且保持靈活性,當然這不是不可能有人想到這一點,並且開始了一個插件,但是我還沒有聽說過一個系統能夠完成所有這自動(還)。 – Sander