我有一个 Ember 应用程序,其侧边栏包含数量可变的小部件。我正在使用 Ember.ContainerView 来存储小部件 View 。
填充容器 View 效果很好(所有小部件都按其应有的方式呈现)。
问题是,当我想更新ContainerView时(因为应用程序状态发生变化,应该加载新的小部件),无法删除当前的childViews.. . 如果我为每个 childView 调用 view.removeChild(child)
和 child.remove()
,则实际上只有少数被删除。
我认为这与 Ember 为每个 remove()
调用更新 DOM 有关,但我无法弄清楚。我还尝试过仅使用 this.set('childViews',[])
而不是单独删除每个 View ,但这会破坏整个 View 。
我在这里遗漏了什么吗?
代码:
// 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 方法:
http://emberjs.com/api/classes/Ember.ContainerView.html#method_removeAllChildren
this.removeAllChildren() 应该可以解决问题。
关于javascript - 如何清除ContainerView中的所有childView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14538736/