Ember.js:使用@each观察数组属性不起作用

Joh*_*ika 5 arrays ember.js

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

App.ArrayProxy = Ember.ArrayProxy.extend({

  i: 0,

  foo: function(){

    console.log('foo called');

    return ++this.i;

  }.property('content.@each')

});
Run Code Online (Sandbox Code Playgroud)

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

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

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

有什么办法绕过这个问题?

Kin*_*n2k 6

你需要使用一个setter(或内置的incrementProperty),如果你关心名字的变化我添加了名字.

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

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

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

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

  • 不,不幸的是,没有内置机制来观察对象上是否有任何属性更改.您可以将它们全部转储到单个计算属性中,并在需要该功能时观察它. (3认同)