Sorting without the ArrayController


#1

I have a route that represents a single group of users. Since it represents one group (and its parent all groups), I am using an ObjectController. Within this group the current user is able to add other users to the group. So among other things I need to sort all of the users by name, but unlike array controllers, object controllers do not have the sortBy method. This route represents a single group, so it wouldn’t make sense to have the controller type reflect this list of users that can be added.

Are there other methods for sorting objects in Ember by their properties, without using the sortBy method in array controllers?

Is there a better way to break this up so that the list of friends can have its own (array) controller and be included in the group template?


#2

Might want to take a look at this discussion, as I think it answers what you’re trying to do:


#3

@Spencer_Price So should I use another route’s controller to sort the users?


#4

Essentially, yes. Though, to clarify: the other controller does not need to be a controller associated with another route. It can be any named controller.

So, even if you did not have a Carpoolers route anywhere, this would still work:

App.CarpoolersController = Ember.ArrayController.extend({
  // Carpoolers controller stuff
});

App.ApplicationRoute = Ember.Route.extend({
  model: function() {
    return this.store.find("carpool", 1);
  },
  setupController: function(controller, model) {
    controller.set('model', model);
    this.controllerFor('carpoolers').set('model', model.get('carpoolers');
  }
});

Does that clarify?


#5

Because controller is just a presenter for model, it’s better to keep each controller a single responsibility.

In this case you need 2 controllers, an ObjectController for group and an ArrayController for users. Then you can use render to display a list of users in group’s template.

var GroupController = Em.ObjectController.extend();

var UserController = Em.ArrayController.extend({
  sortProperties: ['name']
});

In your group.hbs template

{{render 'users' users}}