Uncaught Error: childViews is immutable

Uncaught Error: childViews is immutable

这里 jsbin 显示了问题,但是,它没有像在我的计算机上那样识别错误。在我的电脑上,我被告知 Uncaught Error: childViews is immutable

我试图在单击事件时将子视图添加到 containerView,但我被告知子视图(containerView 上的视图数组)是不可变的。你能解释一下如何将 childView 添加到 containerView 吗?我也试了appendChild但是在渲染过程中出现了无法调用的错误提示

在 Ember 的旧版本中,您曾经能够将对象推送到 childViews 数组,例如,如此 jsfiddle 所示,但 api 似乎已更改.

如果不可能,如何在不推送到 containerView 上的 childViews 数组的情况下创建这个 ui?

App = Ember.Application.create({});

App.MainView = Em.View.extend({
    classNames: ['mainView']
});

App.MyContainerView = Em.ContainerView.extend({});

App.AnotherView = Em.View.extend({
    render: function(buffer){
        buffer.push('App.AnotherView ' + this.get('elementId'));
    }
});

App.AddChildViewButton = Em.View.extend({
    tagName: 'button',
    render: function(buffer) {
        buffer.push('Add Child View');
    },
    click: function() {
        var containerView = Em.View.views['my_container_view']
        var childView = containerView.createChildView(App.AnotherView);
        containerView.get('childViews').pushObject(childView);
    }
});

模板

<script type="text/x-handlebars" >
    {{#view App.MainView}}
      <h1>Hello world!</h1>
      <br>
      {{view App.AddChildViewButton}}    
      <br>
      {{view App.MyContainerView elementId="my_container_view"}}
    {{/view}}
</script>

documentation for Ember.ContainerView 所示,您可以通过将对象推送到视图本身而不是其 childViews 属性:[=25= 来将子视图添加到容器视图]

click: function() {
    var containerView = Em.View.views['my_container_view']
    var childView = containerView.createChildView(App.AnotherView);

    // no need for .get('childViews')
    containerView.pushObject(childView);
}

这是显示此内容的文档之一(那里还有一个在 init 方法中使用 this.pushObject(...) 的示例):

var container = Ember.ContainerView.create();
container.append();

var firstView = App.FirstView.create(),
    secondView = App.SecondView.create();

container.pushObject(firstView);
container.pushObject(secondView);

这是一个 jsBin,其中包含您问题中的示例,它按预期运行:

http://jsbin.com/ciwonuqezi/3/

更多信息:

您提供的 link 确实表明 childViews 可写在 ContainerView 中,并显示了向 childViews 添加视图的示例。看起来这已被弃用,以支持 1.0 中的当前方法,并且该页面上的信息刚刚过时。

This page,例如,表示:

ContainerView’s childViews is deprecated

This is another change that will result in a lot less code. Previously, if you had a ContainerView, you could add and remove views from it using the childViews proeprty. Now, the childViews property has been deprecated as the ContainerView itself acts like an Array. So instead of:

看起来 at some point Ember 提供了弃用通知并在尝试修改 ContainerView 的子视图时允许该操作:

ret.replace = function (idx, removedCount, addedViews) {
  if (view instanceof Ember.ContainerView) {
    Ember.deprecate("Manipulating an Ember.ContainerView through its childViews property is deprecated. Please use the ContainerView instance itself as an Ember.MutableArray.");
    return view.replace(idx, removedCount, addedViews);
  }
  throw new Ember.Error("childViews is immutable");
};

但它似乎已被删除,现在它只会抛出您看到的错误。