9

I use a model in Ember.js like this:

App.SomethingRoute = Ember.Route.extend({
  model: function()
  {
      return App.MyData.find();
  }
});

It receives data from MyData. In my data i have a field called "NAME". I would like to display data from MyData in ascendant order by NAME.

I've added a controller (thx. Toran, intuitive) like this:

App.SomethingController = Ember.ArrayController.extend({
  sortProperties: ['NAME'],
  sortAscending: true
});

But my template that is like this:

{{#each model}}
 {{NAME}}
{{/each}}

Still shows unordered list. How to make it right?

2
  • emberjs.com/api/classes/Ember.SortableMixin.html Commented Jul 29, 2013 at 11:58
  • Can you show the implementation of your find method? you need to be using ember objects (not plain JS objects) to get the binding behavior that will re-sort it on the fly Commented Jul 29, 2013 at 16:55

4 Answers 4

18

ArrayController has been removed from Ember (v2.0) since this question was asked. Here is how you would achieve the same without using an ArrayController:

export default Ember.Controller.extend({
  sortProperties: ['name:asc'],
  sortedModel: Ember.computed.sort('model', 'sortProperties')
});

And then:

{{#each sortedModel as |thing|}}
 {{thing.name}}
{{/each}}

And here is the documentation for Ember's computed sort macro.

Sign up to request clarification or add additional context in comments.

1 Comment

what if I will change sorting order? How to rerender models?
15

Since the ArrayController includes the SortableMixin (already mentioned in the comment from @ianpetzer), you can set the properties you want to sort on in sortProperties.

App.SomethingController = Ember.ArrayController.extend({
  sortProperties: ['name'],
  sortAscending: true
});

5 Comments

i've updated question because i can not make it work. Please help. thx.!
@tomaszs, there must be something else going on in your app that does not make it work, can you update you question with a jsbin/jsfiddle with your setup?
your answer helped me, i needed only to change {{#each model}} to {{#each controller}} also. thx
I had to change controller.content to controller.arrangedContent in my template in order for sorting to work.
ArrayController was removed in Ember 2. See my answer for an updated approach.
6

Make sure you are using {{#each controller}}, not {{#each model}}, since the Controller will have it own copy of the model collection that it sorts and presents to the template.

<!-- ************************************************************************ -->
<script type="text/x-handlebars" data-template-name="tickets">  
<p>
<table id="tickets" class="table table-striped">
<thead>
  <tr>    
    <th {{action "sortByAttribute" "status"}}>Status</th>
  </tr>
</thead>
<tbody>
{{#each controller}}
    <tr>     
      <td>{{#link-to 'ticket' this.id}} {{status}} {{/link-to}} </td>
    </tr>
{{/each}}
</tbody>
</table>  
</p>
</script>

Comments

1
App.SomethingController = Ember.ArrayController.extend({
    sortProperties: ['name'],
    sortAscending: true 
});

Make sure your find method does something like this

App.Person.reopenClass({
    people: [],
    find: function() {
        var self = this;
        $.getJSON('/api/people', function(response) {
            response.forEach(function(hash) {
                var person = App.Person.create(hash);
                Ember.run(self.people, self.people.pushObject, person);
            });
        }, this);
        return this.people;
    }
});

Not this (this will not update the template via binding because it's a vanilla JS object instead of a full blown ember object)

App.Person.reopenClass({
    people: [],
    find: function() {
        var self = this;
        $.getJSON('/api/people', function(response) {
            response.forEach(function(hash) {
                Ember.run(self.people, self.people.pushObject, hash);
            });
        }, this);
        return this.people;
    }
});

1 Comment

i've updated question because i can not make it work. Please help. thx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.