Emberjs sort hasMany association as a computed property

落爺英雄遲暮 提交于 2019-12-11 19:32:56

问题


I have two models, folder and files. A folder has many files. If I say folder.get('files') I get all the files associated with that folder ordered by id. I would like the array of files to be order by something other then the id; lets say createDate. If possible, I do not want to make a computed property that has a different name then files.

Any help would greatly be appreciated?


回答1:


As mentioned in my comment you need to create an computed property files in your controller, which contains your sorting logic.

files: function() {
  var filter = this.get('filter');
  return this.get('model').sortBy(filter);
}.property('filter')

Here is a JSBin: http://emberjs.jsbin.com/pojaxikayu/3/edit, demonstrating how it works. The example is very basic and should be extended according to your specific needs.

You also can use Ember.SortableMixin in you controller. Here is the example from above using the mixin: http://emberjs.jsbin.com/hebisorexo/2/edit



来源:https://stackoverflow.com/questions/30691579/emberjs-sort-hasmany-association-as-a-computed-property

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