How do I sort models using only a route in Ember?

后端 未结 2 794
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 13:03

In the past you could use ArrayControllers (deprecated in 1.13.0), and we know that shortly controllers won\'t be recommended in ember. Is it currently possible to sort my m

2条回答
  •  执笔经年
    2021-01-13 13:30

    New answer post Ember 2.0

    Although the question still stands and torazaburo's answer works great before Ember 2.0. The best answer now is "don't sort a model using only a route" - instead do the sorting in a controller, or if you don't want to use a controller then in a component.

    There is a big 'gotcha' with the reload behaviour post Ember 2.0. If there are already records in the store, and you do not specify { reload: true } in the options of findAll, then the findAll method will instantly resolve with those records, meaning the then only sorts with those records you already had. So your model could return with a limited number of records while the actual background request is still going on. See DS Store docs for further info.

    The improved code based on the previously accepted answer is therefore:

    export default Ember.Route.extend({
      model: function() { 
        return this.store.findAll('order', { reload: true }).
          then(orders => orders.sortBy('name'));
        }
    });
    

    But as previously mentioned, I think the best course of action is to not rely solely on the route, but instead use computed sort in either a controller or a component.

提交回复
热议问题