lundi 3 août 2015

How to sort Ember Data model output?

I've searched and searched, but all the examples seem either outdated, aren't in the file structure enforced by Ember CLI, etc.

Anyway, say I have a model in app/models/employee.js:

export default DS.Model.extend({ firstname: DS.attr('string'), lastname: DS.attr('string'), });

And a route in app/routes/employees.js:

export default Ember.Route.extend({ model: function() { return this.store.findAll('employee'); }, });

And a template in app/routes/employees.hbs:

{{#each model as |employee|}} <tr><td>{{employee.firstname}}</td><td>{{employee.lastname}}</td></tr> {{/each}}

What do I need to add to sort that table by firstname, for example?
I gather I'm supposed to use Ember.computed.sort(), something like:

sortedEmployee: Ember.computed.sort('employees', ['firstname'])

And then do {{#each sortedEmployee as ...}} in the template, but I'm apparently not defining sortedEmployee in the right place.




Aucun commentaire:

Enregistrer un commentaire