javascript - 如何清除ContainerView中的所有childView?

标签 javascript ember.js

我有一个 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/

相关文章:

javascript - 如何在 ES6 类中声明公共(public)函数?

javascript - remove() on img with ng-src, border remains?

javascript - 如何禁用向左滚动?

javascript - 全局变量在函数作用域内未定义

javascript - ember.js 教程 : is really working Mirage addon?

ember.js - 为什么使用 itemController 会呈现空项目的集合?

javascript - 在 Ember.js 中加载关联模型

javascript - 如何区分 AngularJS 中注入(inject)模块的命名空间?

ember.js - 从 ember 数据 json 中删除模型名称

javascript - 在 Ember 中(重新)呈现 View 模板时如何收到通知?