Est-il possible d'observer des tableaux dans des tableaux en utilisant @each?

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

  •  28-10-2019
  •  | 
  •  

Question

J'ai un contrôleur qui contient un tableau de "choses".Dans chacun de ces éléments se trouve un tableau de "sous-éléments".Je voudrais créer une propriété calculée qui contient tous les sous-éléments en toutes choses (un tableau aplati de sous-éléments).

Ma propriété calculée dépend de things.@each.subthings.length.Je trouve que si je définit la propriété subthings d'une chose, ma propriété calculée est mise à jour.Cependant, si j'appelle pushObjects pour ajouter de nouvelles données à mon tableau existant subthings, ma propriété calculée ne se met pas à jour.

J'ai créé un jsfiddle pour le démontrer.Le code est le suivant:

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

Merci!

Était-ce utile?

La solution

Ajouter un autre @each à la liste des propriétés .property('things.@each.subthings.@each.length')

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top