Emberjs排序的hasMany协会作为计算财产 [英] Emberjs sort hasMany association as a computed property
问题描述
我有两个型号,文件夹和文件。文件夹有许多文件。如果我说 folder.get('文件')
我得到与该文件夹相关的所有文件按id排序。我想文件的排列是为了其他的东西则ID;让说CREATEDATE。如果可能的话,我不想让具有不同的名称,然后一个计算属性文件
。
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
.
任何帮助将大大AP preciated?
Any help would greatly be appreciated?
推荐答案
正如我在评论中提及你需要在你的控制器,其中包含创建一个计算的属性文件
您排序逻辑。
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')
下面是一个JSBin: http://emberjs.jsbin.com/pojaxikayu/3/edit ,展示它是如何工作。这个例子是非常基本的,并根据您的特殊需要应适当延长。
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.
您也可以使用 Ember.SortableMixin
在你的控制器。下面是从上面使用的混入例如: http://emberjs.jsbin.com/hebisorexo/2 /编辑
这篇关于Emberjs排序的hasMany协会作为计算财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!