Ember.js:使用@each 观察数组属性不起作用 [英] Ember.js: Observing array property using @each doesn't work

查看:25
本文介绍了Ember.js:使用@each 观察数组属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,观察 '@each' 意味着我正在观察数组中任何属性的任何更改,但它似乎不起作用.例如:

My understanding is that observing for '@each' means that I'm observing any change to any property in an array, but it doesn't seem to work. For example:

App.ArrayProxy = Ember.ArrayProxy.extend({

  i: 0,

  foo: function(){

    console.log('foo called');

    return ++this.i;

  }.property('content.@each')

});

我也尝试过 .property('@each') 而不是 .property('content.@each'),结果同样令人失望.

I've also tried .property('@each') instead of .property('content.@each') with equally disappointing results.

这是一个演示的jsbin:http://jsbin.com/hagar/5/edit

Here is a jsbin that demonstrates: http://jsbin.com/hagar/5/edit

在演示中,更改数组列表本身(通过单击删除最后一个"按钮)会触发foo"计算属性的刷新,但更改数组中对象的属性不会.

In the demo, changing the array list itself (by clicking the 'Remove Last' button) triggers a refresh of the 'foo' computed property, but changing a property of an object in the array doesn't.

有什么办法可以解决这个问题?

Any way around this problem?

推荐答案

您需要使用 setter(或内置的 incrementProperty),如果您关心名称何时更改,我添加了名称.

You need to use a setter (or the built in incrementProperty), I added name if you care when the name was changed.

  foo: function(){
    console.log('foo called');
    return this.incrementProperty('i');
  }.property('content.@each.name')

如果您不关心它在名称更改时递增,您可以使用 foo.[] ,它只会在添加/删除数组项时显示.

If you don't care about it incrementing when name changes you would use foo.[] which would only show when the array items are added/removed.

  foo: function(){
    console.log('foo called');
    return this.incrementProperty('i');
  }.property('content.[]')

http://jsbin.com/bakoqawi/1/edit

这篇关于Ember.js:使用@each 观察数组属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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