1
votes

in a ember route's model hook the following works fine:

model: function () {
    return this.modelFor('foo').get('bar');
}

Backed by ember-data, I can delete some of foo's bars somewhere else and it will be updated automagically (live array).

Now, I want this to be sorted (and user in sub-routes, so I have to do this in the route, not in the controller).

model: function () {
    return this.modelFor('foo').get('bar')
       .then(function (data) {
        return data.sortBy('baz');
    });
},

... does the job only the first time around, because I'm losing the updating.

Is there a way to write automatic updating sorting in line? What is the ember way to solve this?

2

2 Answers

3
votes

To answer my own question, based on the answer from Gaurav:

 model: function () {
    return this.modelFor('foo').get('bar')
        .then(function (data) {
            return Ember.ArrayProxy.extend({
                arrangedContent: Ember.computed.sort('content', 'props'),
                props: ['baz:asc']
            }).create({
                content: data});
        });
},
2
votes

You can create an Ember Object for your model that has a computed property that is the sorted data.

model: function () {
    return this.modelFor('foo').get('bar')
       .then(function (data) {
            return Ember.Object.extend({
                arrangedData: Ember.computed.sort('model', 'props'),
                props: ['baz:asc']
            }).create({ model: data });
       });
},

The Ember.Object.extend part should probably be extracted somewhere so it can be reused in other routes.