cwarny
cwarny

Reputation: 1007

Ember ArrayController cannot be sorted by property defined in itemController

I want to sort an ArrayController by a property defined/computed in the itemController. See this JSBin. If you sort by firstName (defined in the model), it works fine, but if you sort by lastName (defined in the itemController), it doesn't work. Make sure to play with sortAscending: true or false. Any idea how to make this work?

Here is another simpler JSBin that exhibits the same behavior (the first JSBin is closer to my actual code).

Upvotes: 1

Views: 346

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

The sortable mixin is applied on the content, not on the controllers of the content.

Code: https://github.com/emberjs/ember.js/blob/v1.1.2/packages/ember-runtime/lib/mixins/sortable.js#L72

You'll probably want to add whatever logic you're adding on the controllers to the models.

The particular use case that you mentioned before was best suited on the model. Really the place where you draw the line on controller and model is wishy washy. If the property needs to persist across controllers then you should add it to the model, especially if the controller isn't a singleton controller. If it's a singleton controller and the model never changes underneath it, then the property can live on the controller.

It's important to note that defining a property on the model doesn't mean you have to get it from the server, nor save it to the server.

App.User = DS.Model.extend({
  name : DS.attr(),  // this will be saved to the server
  something: 31      // this isn't a DS attr, it isn't going anywhere
});

As a note, I lied earlier about something.

You can talk to your child controllers from your parent controller.

From inside of the parent controller, you can access the child controllers using objectAt and iterating over the parent controller.

In this example this is the parent controller

  console.log(this.objectAt(0));

  this.forEach(function(itemController){
    console.log(itemController);
  });

http://emberjs.jsbin.com/AQijaGI/1/edit

Upvotes: 1

Related Questions