2012-02-21 5 views
1

私は "物"の配列を含むコントローラを持っています。これらのそれぞれの中には、「副交渉」の配列があります。私は、すべての事柄(下位の平らな配列)のすべての下位条件を含む計算されたプロパティを作成したいと思います。@eachを使って配列内の配列を見ることはできますか?

私の計算されたプロパティは[email protected]に依存します。私がに設定した場合、物のプロパティが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('[email protected]').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); 

ありがとう!

答えて

2

は、うまく機能プロパティリスト.property('[email protected]@each.length')

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

+0

のおかげで、別の@eachを追加します!たぶん私はここで何をしているのかよく分かりません。あなたの解決策は、subthings配列自体の長さではなく、 "subthings"配列の各要素のlengthプロパティを観察しているようです。 – rharper

+3

Emberのドキュメントより:http://emberjs.com/guides/object-model/computed-properties-and-aggregate-data/ "@eachは1つのレベルしか動作しないことに注意してください。todosのようなネストされたフォームは使用できません。 .owner.nameまたはtodos。@ each.owner。@ each.name –

関連する問題