Ember.js: Observing array property using @each doesn't work

拈花ヽ惹草 提交于 2019-12-19 08:12:58

问题


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')

});

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

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

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?


回答1:


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')

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



来源:https://stackoverflow.com/questions/24892950/ember-js-observing-array-property-using-each-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!