2013-02-27 26 views

回答

2

你必須定義額外的 「自然」 屬性

App.Group = DS.Model.extend         
    name: DS.attr 'string'            
    natural_name: (->             
    # Split string into numeric and non-numeric parts 
    # and convert numeric parts to actual numbers 
    # Sort by resulting array of strings and numbers 
    @get('name').split(/([0-9]+)/g).map (str) =>      
     if str.match(/[0-9]+/) then parseInt(str, 10) else str   
).property('name')  



App.GroupsIndexController = Ember.ArrayController.extend 
    sortProperties: ['natural_name'] 
1

如果你想使用SortableMixin你可以做這樣的:

childrenSorted: function() { 
    return Ember.ArrayProxy.createWithMixins(Ember.SortableMixin, { 
    sortProperties: ['name'], 
    content: this.get('model.children') 
    }); 
}.property('[email protected]') 
3

什麼我在我的項目中完成了,是重寫mixin的orderBy函數(https://github.com/emberjs/ember.js/blob/master/packages/ember-runtime/lib/mixins/sortable.js#L52)和這種自然的排序算法取代Ember.compare():https://github.com/overset/javascript-natural-sort

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; 
} 

我爲了能夠更方便地將任何排序功能的排序混入做出了PR,但它被關閉。查看詳情這裏:

https://github.com/emberjs/ember.js/pull/1216 https://github.com/emberjs/ember.js/pull/1562