Can somebody please explain computed.sort to me?


#1

Sorting, it should be easy. Sadly, it has me completely flummoxed. I am simply trying to sort records (reaches in my case) based on the river name and name fields in a data store named reaches. Just make sure I am not missing anything, here is my entire controller.

import Controller from '@ember/controller';
import { computed } from '@ember/object';

export default Controller.extend({

 // linking the url to the query params in the field
 queryParams: ['search'],
 search: '',

 // sort the reaches so they appear in logical order
 sortFields: ['riverName:asc', 'name:asc'],
 modelSorted: computed.sort('reaches', 'sortFields'),

 actions: {

 // this prevents pressing enter from submitting and reloading the page
 preventSubmit(evt) {
 evt.preventDefault();
 return false;
 },

 }

});

Nothing is showing up when I try to display the results in my template.

{{#each modelSorted as |reach|}}
   <tr>
     <td>
       {{#link-to 'reach' reach.id}}
       {{reach.riverName}} - {{reach.name}}
       {{#if reach.riverAlternateName}}
         ({{reach.riverAlternateName}})
       {{/if}}
       ({{reach.difficulty}})
       {{/link-to}}
     </td>
   </tr>
{{/each}}

Incidentally, it was working, just not sorted, when referencing he model directly, so I am really confused. If it is relevant, I am using Ember 2.16.2 with Ember Data 2.16.3. What am I missing?


#2

The first string you’re passing to computed.sort is ‘reaches’, but what is that? It’s not defined on your controller so should that be ‘model.reaches’? Or maybe just ‘model’?


#3

@dknutsen, you nailed it! Using just ‘model’ worked perfectly. I had tried, reach, reaches, but not model. Thank you for helping me get past a Homer Simpson moment…d’oh!


#4

I do stuff like that all the time! Only way I know to look out for it.