Javascript 如何从ContainerView中清除所有子视图?
我有一个带有侧边栏的Ember应用程序,其中包含数量可变的小部件。我正在使用Ember.ContainerView来存储小部件视图 填充容器视图效果很好(所有小部件都按其应有的方式呈现) 问题是当我想更新ContainerView时(因为应用程序状态改变,应该加载新的小部件),当前的子视图不能被删除。。。如果我为每个childView调用Javascript 如何从ContainerView中清除所有子视图?,javascript,ember.js,Javascript,Ember.js,我有一个带有侧边栏的Ember应用程序,其中包含数量可变的小部件。我正在使用Ember.ContainerView来存储小部件视图 填充容器视图效果很好(所有小部件都按其应有的方式呈现) 问题是当我想更新ContainerView时(因为应用程序状态改变,应该加载新的小部件),当前的子视图不能被删除。。。如果我为每个childView调用view.removeChild(child)和child.remove(),实际上只有少数被删除 我认为这与Ember为每个remove()调用更新DOM有关
view.removeChild(child)
和child.remove()
,实际上只有少数被删除
我认为这与Ember为每个remove()
调用更新DOM有关,但我想不出来。我还尝试使用this.set('childViews',[])
而不是单独删除每个视图,但这会破坏整个视图
我是不是遗漏了什么
代码:
// Clear current views
this.get('childViews').forEach(function(_view) {
view.removeChild(_view);
_view.remove();
});
var view = this,
childViews = this.get('childViews');
// Create new views
this.get('controller.content').forEach(function (widgetData) {
childViews.pushObject(App.WidgetView.create({
controller: App.WidgetController.create(widgetData),
...
})
});
Em.ContainerView上有一个removeAllChildren方法:
这个.removeAllChildren()应该可以做到这一点。好的,首先,很抱歉我忽略了这个方法。在某种程度上,它起到了作用。更新后parentView没有更多的子对象,但它们没有从DOM中删除。我尝试使用
Ember.run.sync()
强制DOM更新,但似乎不起作用。我目前正在使用一个相当难看的('#'+this.elementId).html('')
,就在removeAllChildren()
调用之后。。