@eachを使用して配列内の配列を観察することは可能ですか?

StackOverflow https://stackoverflow.com/questions/9381059

  •  28-10-2019
  •  | 
  •  

質問

「物」の配列を含むコントローラーがあります。これらのそれぞれの中には、「サブチング」の配列があります。すべてのもののすべてのサブソングを含む計算されたプロパティ(サブソウの平坦化された配列)を作成したいと思います。

私の計算されたプロパティは依存します things.@each.subthings.length. 。私はそれを見つけます 設定 subthings 物のプロパティ、私の計算されたプロパティが更新されます。しかし、私が電話した場合 pushObjects 私に新しいデータを追加します 既存 subthings 配列、私の計算されたプロパティは更新されません。

私は作成しました jsfiddle デモンストレーション。コードは次のとおりです。

App = Em.Application.create({});

App.controller = Em.Object.create({
    things: [
        Em.Object.create({subthings:[]}),
        Em.Object.create({subthings:[]}),
        Em.Object.create({subthings:[]})        
    ],

    allSubThings : function() {
        var things = this.get('things');
        var results = [];
        things.forEach( function(thing)  {
            results.pushObjects( thing.get('subthings') );
        });
        return results;
    }.property('things.@each.subthings.length').cacheable()        
});


setTimeout(function() {
    var things = App.controller.get('things');
    // This works:
    things.objectAt(0).set('subthings',[1,2,3]);
}, 1000);

setTimeout(function() {
    var things = App.controller.get('things');
    // This does not:
    things.objectAt(1).get('subthings').pushObjects([1,2,3]);
}, 2000);

ありがとう!

役に立ちましたか?

解決

別のものを追加します @each プロパティリストに .property('things.@each.subthings.@each.length')

http://jsfiddle.net/tomwhatmore/pdqet/3/

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top