Ember 1.13.2 ArrayController replacement

I think recreating SortableMixin / ArrayController is contradictory to “everything should be a component” because mixins and controllers are not components.

A quick explanation of “explicit transformation to a model”:

You pass in a model to your component:

{{my-component data=model}}

The model you pass in either is or contains an array. Let’s assume the latter, and say that data.items (in the context of the component) is an array you would like to sort using a property ‘date’. Then in your component you create a computed property ‘sortedItems’:

sortedItemsProps: ['date:asc'],
sortedItems: Ember.computed.sort('data.items', 'sortedItemsProps'),

Then in your component’s template somewhere you can #each over sortedItems:

{{#each sortedItems as |item|}}
     {{another-component-acting-like-an-item-controller data=item}}
{{/each}}

This is ‘explicit’ in the sense there is no abstraction… you can see exactly what is going on and no details are hidden from you.

It is a ‘transformation’ in the sense that you are transforming ‘data.items’ to ‘sortedItems’. You can use additional transformations through by composing computed properties.

And of course what you are transforming is the model or a part thereof which originally came from a route.

Sorry, I missed the fact that you can specify a property. I thought you could only pass in a function. In this case, Ember.computed.sort actually does everything that SortableMixin used to provide.

…just want to provide some feedback on this.

I’m now using Ember.computed.sort() successfully instead of my earlier suggested ArrangedConrentFix.

I solved it like this:

  arrangedContent: function() {
    return Ember.ArrayProxy.extend(Ember.SortableMixin).create({
      sortProperties: ['date'],
      sortAscending: false,
      content: this.get('model')
    });
  }.property('model'),

but perhaps Ember.computed.sort() is better? Not sure.

1 Like