Sort nested array from model in Ember?

So, I have a model in Ember that generates a hash with three objects. One of the objects is an array of objects with another array inside each object. I need to sort this very internal array, but I am having problems with this.

Here are my models.

App.Person = DS.Model.extend ({
    first_name: DS.attr('string'),
    last_name: DS.attr('string'),
    age: DS.attr('string'),
    gender: DS.attr('string'),
    innerMostArray: DS.hasMany('innerMostObject')
});

App.innerMostObject = DS.Model.extend ({
    person_id: DS.belongsTo('person'),
    attr1: DS.attr('string'),
    attr2: DS.attr('string')
});

Here is my route

App.NestedArrayRoute = Ember.Route.extend({
    model: function(params) {
        return Ember.RSVP.hash({
            object1: this.store.find('object1', params.object1_id),
            people: this.store.all('person'),
            object3: this.store.all('object3')
        });
    },
    afterModel: function(model, transition) {
        model.people.forEach(function(item, index, enumerable){
            var innerMostArray = item.get('innerMostArray');
            var sortedArray = innerMostArray.sortBy('attr1', 'attr2');
        });
        model.people.update();
    } 
});

I know that I hardly do this right, but I just don’t know how to sort this nested array. I saw examples of array controllers, but I don't know how to use them to sort this nested array. If someone could give an example of how to do this, it will be very helpful. Thank.

+4
source share
2 answers

Kalmans, , :

App.Person = DS.Model.extend({
   name: DS.attr('string'),
   fruits: DS.hasMany('fruit', {async: true}),
   fruitSorting: ['title', 'color'],
   sortedFruits: Ember.computed.sort('fruits', 'fruitSorting')
});

: http://emberjs.jsbin.com/manutu/1/edit?html,js,output

+5

- :

App.Person = DS.Model.extend({
  name: DS.attr('string'),
  fruits: DS.hasMany('fruit', { async: true }),
  sortedFruits: function(){
    var fruits = this.get('fruits');
    return fruits.sortBy('title', 'color');
  }.property('fruits.@each.title', 'fruits.@each.color')
});

+3

All Articles