“不能调用方法”破坏未定义的“在arrayWillChange中 [英] "Cannot call method 'destroy' of undefined" in arrayWillChange
问题描述
不能调用方法'destroy'未定义的$ Ember源的 arrayWillChange
方法中的c $ c>错误: for(idx = start + removedCount - 1; idx> = start; idx--){
childView = childViews [idx];
if(removedAll){childView.removedFromDOM = true; }
childView.destroy(); < - childView未定义
}
我以前从未有过这个问题。当我从数组中删除一个项目时,这不会发生。只有添加。以下是JSBin的一个链接,我试图复制该问题。错误不会被抛出,但模板也不会更新。
解决方案 编辑:
您正在调用 array.push
而不是 array.pushObject
- 后者是一个绑定感知的Ember.js方法,这意味着它将自动更新绑定。 handlebars模板助手 {{#each filters}}
是与控制器的filters数组的绑定,模板需要知道才能更新底层数组的更新。 push
不会将绑定更新,而是 pushObject
。
这是一个工作示例(所有我做的是将push推送到pushObject): http://jsbin.com/asemul/6/
这是一个很常见的错误 - 通常我发现如果我的模板不与基础对象同步,因为绑定有问题,所以我开始看看。
END EDIT / p>
- 我不认为你应该直接设置
removedFromDOM
code> childView.remove()
后跟 destroy()
。 ContainerView
或 的CollectionView
?这两个视图都支持子视图数组,并可以更加健壮地实现您希望做的更少的代码。
I have what I think is a pretty standard array/template relationship setup, but when I push a new item into the array I get the above mentioned Cannot call method 'destroy' of undefined
error in the arrayWillChange
method of the Ember source:
for (idx = start + removedCount - 1; idx >= start; idx--) {
childView = childViews[idx];
if (removingAll) { childView.removedFromDOM = true; }
childView.destroy(); <-- childView is undefined
}
I have never had this issue before. This doesn't happen when I remove an item from the array. Only on addition. Below is a link for a JSBin where I tried to duplicate the issue. The error doesn't get thrown but the template doesn't update either.
EDIT:
You're calling array.push
instead of array.pushObject
-- the latter is an Ember.js method that is binding aware, which means it will automatically update bindings for you. The handlebars template helper {{#each filters}}
is a binding to the filters array of the controller, and the template needs to know to update when the underlying array is updated. push
doesn't tell the binding to update, but pushObject
does.
Here's a working example (all I did was change push to pushObject): http://jsbin.com/asemul/6/
This is a pretty common mistake -- usually, I find that if my templates aren't synchronized with the underlying object, it's because something's wrong with the bindings, so that's where I start looking.
END EDIT
- I don't think you should be setting
removedFromDOM
directly -- try usingchildView.remove()
followed bydestroy()
. - I'm not sure what the context is, but have you looked at
ContainerView
orCollectionView
? Both of those views have support for arrays of child views and may accomplish what you're looking to do both more robustly and with less code.
这篇关于“不能调用方法”破坏未定义的“在arrayWillChange中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!