2015-04-02 231 views
0

companiesships都具有name屬性。但我不明白我怎麼可以在這個代碼中使用sortProperties按名稱排序屬性

的app/controllers/index.js

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    selectedCompany: null, 

    companies: function(){ 
    var model = this.get('model.companies'); 
    return model; 
    }.property(), 

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

應用程序/路由/ index.js

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model: function() { 
    return { 
     companies: this.store.find('company'), 
     ships: this.store.find('ship') 
    }; 
    } 
}); 

如何通過name屬性對companiesships進行排序?

回答

1

使用sortBy函數。就像這樣:

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    selectedCompany: null, 

    sortedCompanies: function(){ 
    var model = this.get('model.companies').sortBy('name'); 
    return model; 
    }.property('model.companies'), 

    sortedShips: function(){ 
    var model = this.get('model.ships').sortBy('name'); 
    return model; 
    }.property('model.ships') 
}); 

記得把你觀察到.property()財產,否則它的緩存不會失效模式發生變化時。