问题
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