【问题标题】:How to clear all childViews from ContainerView?如何从 ContainerView 中清除所有 childView?
【发布时间】:2013-01-26 16:07:13
【问题描述】:

我有一个带有边栏的 Ember 应用程序,其中包含可变数量的小部件。我正在使用 Ember.ContainerView 来存储小部件视图。

填充容器视图可以正常工作(所有小部件都按应有的方式呈现)。

问题是当我想更新ContainerView时(因为应用程序状态改变,应该加载新的widgets),当前的childViews不能被移除.. . 如果我为每个 childView 调用 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),
           ...
        })
    });

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    Em.ContainerView 上有一个 removeAllChildren 方法:

    http://emberjs.com/api/classes/Ember.ContainerView.html#method_removeAllChildren

    this.removeAllChildren() 应该可以解决问题。

    【讨论】:

    • 好的,首先,很抱歉我忽略了这种方法。它起到了作用,有点。更新后,父视图没有更多子视图,但它们并未从 DOM 中删除。..
    • 我尝试使用Ember.run.sync() 强制 DOM 进行更新,但这似乎不起作用。我目前正在使用一个相当丑陋的('#'+this.elementId).html('')removeAllChildren() 调用之后实现这一目标..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    相关资源
    最近更新 更多