wintermeyer
wintermeyer

Reputation: 8318

Sort by name attribute

companies and ships both have a name attribute. But I don't understand how I can use sortProperties in this code:

app/controllers/index.js

import Ember from 'ember';

export default Ember.Controller.extend({
  selectedCompany: null,

  companies: function(){
    var model = this.get('model.companies');
    return model;
  }.property(),

  ships: function(){
    var model = this.get('model.ships');
    return model;
  }.property()
});

app/routes/index.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return {
      companies: this.store.find('company'),
      ships: this.store.find('ship')
    };
  }
});

How can I sort companies and ships by the name attribute?

Upvotes: 0

Views: 54

Answers (1)

albertjan
albertjan

Reputation: 7817

Use the sortBy function. Like this:

import Ember from 'ember';

export default Ember.Controller.extend({
  selectedCompany: null,

  sortedCompanies: function(){
    var model = this.get('model.companies').sortBy('name');
    return model;
  }.property('model.companies'),

  sortedShips: function(){
    var model = this.get('model.ships').sortBy('name');
    return model;
  }.property('model.ships')
});

Remember to put the property you observe into the .property() otherwise it's cache won't be invalidated when the model changes.

Upvotes: 1

Related Questions