2

Okay, so I have a controller with it's model set to the following:

App.MyModel = DS.Model.extend({
    myOtherProperty: attr(),
    mySortKey: attr(),
    mySubCollection: function () {
        return this.store.filter('MyOtherModel', function (myOtherModel) {
            //Some basic filtering logic (it's actually more complex than this)
            return myOtherModel.get('someProperty') == this.get('myOtherProperty');
        });
    }.property('myOtherProperty')
})

The actual filter function isn't important - what is important is that the 'mySubCollection' computed property returns a DS.PromiseArray object. The controller for the above model is of type 'Ember.ObjectController' as it is only displaying a single object.

The template that displays this property uses an {{each}} helper to do so:

MyTemplate:

{{#each mySubCollection}}
    {{view App.MyView}}
{{/each}}

Now - I need to sort this promise array by the 'mySortKey' property in descending order. How can I do this? The documentation says that DS.PromiseArray extends from Ember.Array, but adding 'sortBy('mySortKey:desc')' at the end of the computed property for 'mySubCollection' causes it to break and not work at all - which makes sense because I would be calling 'sortBy' on a promise not an array.

I've looked at a bunch of different ideas and the one that keeps coming up is to switch to using an ArrayController. I can do that, but it isn't ideal as it's adding a bunch of complexity on top of a pretty simple setup that works great already (aside from sorting).

Any thoughts?

2 Answers 2

9

I haven't been quite able to figure out why sortBy doesn't work on the PromiseArray. But here's what I use as a workaround:

sortedCollection: function() {
    return Em.ArrayProxy.createWithMixins(Em.SortableMixin, {
        content: this.get('mySubCollection'),
        sortProperties: [this.get('mySortKey')],
        sortAscending: false
    });
}.property('mySubCollection', 'mySortKey')
4
  • I like this idea, and that's a nice way of showing mixins...thanks! I'll give this a try and if it works I'll give you the answer Feb 27, 2014 at 21:07
  • is this still the known way of doing things?
    – Anthony
    Dec 4, 2014 at 15:03
  • 1
    @Anthony Whether or not it's the best way of doing things, I don't know. But I know it still works and I still do it occasionally.
    – GJK
    Dec 4, 2014 at 20:14
  • createWithMixins method is deprecated. Now it's Em.ArrayProxy.extend(Em.SortableMixin).create({ ... })
    – Yujun Wu
    Jul 29, 2015 at 21:11
4

Ember.SortableMixin has been deprecated, use Ember.computed.sort instead.

App.MyModel = DS.Model.extend({
    myOtherProperty: attr(),
    mySortKey: attr(),
    mySubCollection: ...
    sortedCollection: Ember.computed.sort('mySubCollection', function(a, b){
        # compare logic
        return a.get('time') - b.get('time');
    }
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.