Search code examples
ember.jsember-dataember-cli

Sort by two properties using different sort-directions with SortableMixin


I'm retrieving posts using findAll (because it auto-updates when new posts are pushed to the store).

Therefore I'm doing the sorting in the controller using the SortableMixin. You can specify multiple sort-properties but the sort-direction for both properties are different. It would isPublished -> Ascending, createdAt -> Descending (Show drafts first, then the rest starting with the latest).

sortProperties: ['isPublished','createdAt'],
sortAscending: false

How can I make this work without sacrificing the auto-updating template?


Solution

  • According to this article, you can do something like:

    items: [ /* blah blah */ ],
    sortProperties: ['isPublished:asc', 'createdAt:desc'],
    sortedItems: Ember.computed.sort('items', 'sortProperties')
    

    Personally I always just write a custom sortFunction when using the SortableMixin, but this looks like a nifty shortcut.