“不能调用方法”破坏未定义的“在arrayWillChange中 [英] "Cannot call method 'destroy' of undefined" in arrayWillChange

查看:102
本文介绍了“不能调用方法”破坏未定义的“在arrayWillChange中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我认为是一个非常标准的数组/模板关系设置,但是当我把一个新的项目推入数组,我得到上面提到的不能调用方法'destroy'未定义的 arrayWillChange 方法中的c $ c>错误:

  for(idx = start + removedCount  -  1; idx> = start; idx--){
childView = childViews [idx];
if(removedAll){childView.removedFromDOM = true; }
childView.destroy(); < - childView未定义
}

我以前从未有过这个问题。当我从数组中删除一个项目时,这不会发生。只有添加。以下是JSBin的一个链接,我试图复制该问题。错误不会被抛出,但模板也不会更新。



http://jsbin.com/asemul/2

解决方案

编辑



您正在调用 array.push 而不是 array.pushObject - 后者是一个绑定感知的Ember.js方法,这意味着它将自动更新绑定。 handlebars模板助手 {{#each filters}} 是与控制器的filters数组的绑定,模板需要知道才能更新底层数组的更新。 push 不会将绑定更新,而是 pushObject



这是一个工作示例(所有我做的是将push推送到pushObject): http://jsbin.com/asemul/6/



这是一个很常见的错误 - 通常我发现如果我的模板不与基础对象同步,因为绑定有问题,所以我开始看看。



END EDIT / p>


  1. 我不认为你应该直接设置 removedFromDOM code> childView.remove()后跟 destroy()

  2. 我不知道上下文是什么,但你看过 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.

http://jsbin.com/asemul/2

解决方案

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

  1. I don't think you should be setting removedFromDOM directly -- try using childView.remove() followed by destroy().
  2. I'm not sure what the context is, but have you looked at ContainerView or CollectionView? 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆