Javascript 未捕获错误:ChildView是不可变的
这是一个显示问题的示例,但是,它不能以与我的计算机相同的方式识别错误。在我的计算机上,我被告知Javascript 未捕获错误:ChildView是不可变的,javascript,ember.js,Javascript,Ember.js,这是一个显示问题的示例,但是,它不能以与我的计算机相同的方式识别错误。在我的计算机上,我被告知未捕获错误:childview是不可变的 我试图在单击事件时将childview添加到containerView,但我被告知childview(containerView上的视图数组)是不可变的。您能解释一下如何将子视图添加到containerView中吗?我还尝试了appendChild,但出现了一条错误消息,在渲染过程中无法调用它 例如,在旧版本的Ember中,您曾经能够将对象推送到ChildVie
未捕获错误:childview是不可变的
我试图在单击事件时将childview添加到containerView
,但我被告知childview(containerView上的视图数组)是不可变的。您能解释一下如何将子视图添加到containerView中吗?我还尝试了appendChild
,但出现了一条错误消息,在渲染过程中无法调用它
例如,在旧版本的Ember中,您曾经能够将对象推送到ChildView数组上,如图所示,但api似乎已经更改
如果不可能,如何创建此ui而不推送到containerView上的childViews数组
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>
{{{#view App.MainView}
你好,世界!
{{view App.AddChildViewButton}
{{view App.MyContainerView elementId=“my_container_view”}
{{/view}
如中所示,您可以通过将对象推送到视图本身,而不是推送到其子视图
属性,将子视图添加到容器视图:
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(…)
):
下面是一个jsBin,其中包含您的问题中的示例,其功能符合预期:
更多信息:
您提供的链接确实说明childview
可在ContainerView
中写入,并显示了向childview
添加视图的示例。看起来这是在1.0中为支持当前的方法而弃用的,并且该页面上的信息已经过时
例如,他说:
ContainerView的子视图不推荐使用
这是另一个将导致代码大大减少的更改。以前,如果您有一个ContainerView,您可以使用childViews项目从中添加和删除视图。现在,childViews属性已被弃用,因为ContainerView本身就像一个数组。因此,不是:
看起来Ember在试图修改ContainerView的ChildView时提供了一个弃用通知并允许该操作:
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");
};
但是它似乎已经被删除了,现在只是抛出了您看到的错误。谢谢,在下面的链接中,它说
childview
属性是可写的@Leahcim,当然可以,但看起来该信息已经过时了。请看我的编辑。
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");
};