Backbone.js的 - 删除所有子意见 [英] Backbone.js - Remove all sub views
问题描述
我有一个顶层的浏览量,这将重新渲染本身每当路由变化。我已经嵌入到这个浏览量许多嵌套子视图。如果我是重新渲染网页浏览,我需要删除/解除绑定的网页浏览以及所有嵌套子视图或者我只需要删除/解除绑定的浏览量?如果我需要删除/解除绑定所有的子观点,什么是去这样做的最佳方式?
I have a top level PageView that will re-render itself whenever the route changes. I have many nested sub-views embedded into this PageView. If I was to re-render PageView, do I need to remove/unbind all the nested sub-views along with the PageView or do I only need to remove/unbind the PageView? If I need to remove/unbind all the sub-views, what is the best way to go about doing this?
推荐答案
是的,你需要正确地删除和解除他们:
Yes, you need to properly remove and unbind them:
<一个href=\"http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/\">http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/
最简单的方式做,这是存储在父视图中的子视图数组。然后在父视图中的关闭
方法,遍历数组,并呼吁对孩子意见的关闭
方法:
The easy way to do this is to store an array of your sub-views in the parent view. Then in a close
method on the parent view, loop through the array and call a close
method on the child views:
ParentView = Backbone.View.extend({
initialize: function(){
this.childViews = [];
},
render: {
for (var i = 0; i < 10; i++){
var childView = new ChildView();
// do stuff with the child view
this.childViews.push(childView);
}
},
close: function(){
this.remove();
this.unbind();
// handle other unbinding needs, here
_.each(this.childViews, function(childView){
if (childView.close){
childView.close();
}
})
}
});
请务必拨打关闭
方法上,当你准备好了要删除/替换父视图。这将确保所有的孩子被正确地清理(假设所有的人都有自己的关闭
法)。
Be sure to call the close
method on the parent view when you are ready for it to be removed / replaced. This will ensure all of the children are cleaned up properly (assuming all of them have their own close
method).
这篇关于Backbone.js的 - 删除所有子意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!