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

查看:224
本文介绍了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('@每个),而不是.property('的内容。@每一个')用同样令人失望的结果。

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

在演示中,改变了数组列表本身(通过点击删除最后一个按钮)触发'富'计算财产的刷新,但在数组中改变一个对象的属性没有。

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

如果你不关心,当名称更改你可以使用富递增它。[] 这将只显示当数组项目添加/删除。

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天全站免登陆