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

编程入门 行业动态 更新时间:2024-10-28 10:32:30
本文介绍了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/hagar/5/edit

Here is a jsbin that demonstrates: http://jsbin/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/bakoqawi/1/edit

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

更多推荐

[db:关键词]

本文发布于:2023-03-31 10:38:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/801408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   不起作用   属性   Ember   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!