Javascript 如何使用Ember.js SortableMixin按自然顺序排序?
Javascript 如何使用Ember.js SortableMixin按自然顺序排序?,javascript,sorting,ember.js,natural-sort,Javascript,Sorting,Ember.js,Natural Sort,您必须定义其他“自然”属性 如果要使用SortableMixin,可以这样做: childrenSorted: function() { return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, { sortProperties: ['name'], content: this.get('model.children') }); }.property('model.children.@each.name')
您必须定义其他“自然”属性
如果要使用SortableMixin,可以这样做:
childrenSorted: function() {
return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
sortProperties: ['name'],
content: this.get('model.children')
});
}.property('model.children.@each.name')
在我的项目中,我所做的是覆盖mixin()的orderBy函数,并用以下自然排序算法替换Ember.compare() 我做了一个PR,以便更容易在可排序的mixin中插入任何排序函数,但它被关闭了。详情如下: 从Ember Data 1.0开始(可能更早),您可能希望使用
Ember.computed.sort
:
childrenSorted: function() {
return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, {
sortProperties: ['name'],
content: this.get('model.children')
});
}.property('model.children.@each.name')
orderBy: function (item1, item2) {
var result = 0,
sortProperties = this.get('sortProperties'),
sortAscending = this.get('sortAscending');
Ember.assert("you need to define `sortProperties`", !!sortProperties);
sortProperties.forEach(function (propertyName) {
if (result === 0) {
naturalSort.insensitive = true;
result = naturalSort(Ember.get(item1, propertyName), Ember.get(item2, propertyName));
if ((result !== 0) && !sortAscending) {
result = (-1) * result;
}
}
});
return result;
}