~ read.
Ember Array Sorting without ArrayController

Ember Array Sorting without ArrayController

One thing I come up against in my work is the need to sort arrays in Ember in cases where using an ArrayController just will not work. We have a few very complex routes that contain a lot more information than Ember's amazingly helpful ArrayController can easily maintain. In one particular route, we have four Ember Data calls that pull in arrays that each need sorting.

We could create a view, controller, and model for each of these pieces of data, but that's just way more than is necessary for our purposes. Google "Ember sort array" and you get almost exclusively information about ArrayController's sortProperties property. There has to be a way, though, to get the benefits of sortProperties without needing to use ArrayController. And there is!

Here's how I do it with Ember Data. Pardon my coffeescript.

@store.find('item', filter).then (items) ->
      items = Ember.ArrayProxy.extend(Ember.SortableMixin).create(items)
      items.set 'sortProperties', ['display_order', 'name']

This allows us to sort first on some arbitrary value display_order and then alphabetically. I prefer the extend(mixins).create() method, but you are welcome to use ArrayProxy's createWithMixins().

I hope this helps some people who might be new to Ember trying to find their way around.

Hit me up @mattahorton on Twitter if you think I can help.

comments powered by Disqus