Ember - Sort Model after findAll

一个人想着一个人 提交于 2019-12-24 10:35:14

问题


I'm using Emberfire in my app, and I'm trying to findAll stats, and then sort that model by a key, like in the following example. But, when I sort this way I lose the ability to see real time updates in my template and I have to reload the page to see the new/updated data in the view.

 model() {
    return this.store
      .findAll('stats', {
        reload: true,
        backgroundReload: true
      })
      .then(stats => stats.sortBy('date'));
  }

回答1:


You have to define a computed property at your controller or a component, which returns the sorted stats. Do not sort your data at route's model hook. Just return the promise of findAll.

For example:

//controller.js or component.js 
sortedStats: computed('model.@each.date', function() {
  return this.get('model').sortBy('date');
})

Furthermore ember offers a sort macro:

import { sort } from '@ember/object/computed';

By using it you can solve your requirement more elegant:

// ...
this.init() {
  this._super(...arguments);
  this.set('sortDefinition', ['date:asc']);
}
sortedStats: sort('model.@each.date', 'sortDefinition')
// ...


来源:https://stackoverflow.com/questions/53489283/ember-sort-model-after-findall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!