1

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).

1 Answer 1

1

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

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

3 Comments

The reason the property is defined in the controller rather than the model is that the property can be modified by user interaction. How would you proceed in that case then?
Is that the right link? I feel like nothing is different from my link
Interesting. Thanks for your answer. My real use case is the following: the user can manipulate a range slider, which sets the value of 2 parameters, which in turn are used to modify some numerical property in the objects of an array. That property is the one I want to sort the array by. The way I was proceeding was to define these parameters in the controller. If I understand correctly, I should rather define them in the model and modify the model directly? I see how that is possible. Intuitively, though, I would have thought the controller to be a more appropriate place for such parameters.

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.