按名称属性排序

时间:2015-04-02 18:21:10

标签: ember.js ember-data ember-cli

companiesships都有name属性。但我不明白如何在此代码中使用sortProperties

应用/控制器/ 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')
    };
  }
});

如何按companies属性对shipsname进行排序?

1 个答案:

答案 0 :(得分: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(),否则当模型更改时,它的缓存不会失效。