Ember ArrayController cannot be sorted by property defined in itemController

老子叫甜甜 提交于 2019-12-24 07:01:51

问题


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:


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



来源:https://stackoverflow.com/questions/20082228/ember-arraycontroller-cannot-be-sorted-by-property-defined-in-itemcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!