È possibile osservare gli array all'interno di array usando @ech?
-
28-10-2019 - |
Domanda
Ho un controller che contiene una serie di "cose". All'interno di ciascuna di queste cose c'è una matrice di "sottoproni". Vorrei creare una proprietà calcolata che contenga tutti i sottotedini in tutte le cose (una serie appiattita di sottoproni).
La mia proprietà calcolata dipende da things.@each.subthings.length
. Lo trovo se io impostare il subthings
Proprietà di una cosa, la mia proprietà calcolata viene aggiornata. Tuttavia, se chiamo pushObjects
Per aggiungere nuovi dati al mio esistente subthings
Array, la mia proprietà calcolata non si aggiorna.
Ho creato un jsfiddle dimostrare. Il codice è il seguente:
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);
Grazie!
Soluzione
Aggiungi un altro @each
all'elenco delle proprietà .property('things.@each.subthings.@each.length')