灰烬-在findAll之后对模型进行排序 [英] Ember - Sort Model after findAll
问题描述
我在我的应用程序中使用Emberfire,并尝试 findAll
统计信息,然后按键对该模型进行排序,如以下示例所示。但是,当我以这种方式进行排序时,我无法查看模板中的实时更新,因此必须重新加载页面才能在视图中查看新数据/更新数据。
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'));
}
推荐答案
您必须定义一个计算式控制器或组件上的属性,它返回已排序的统计信息。不要在路线的模型挂钩处对数据进行排序。只需返回findAll的承诺即可。
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.
例如:
//controller.js or component.js
sortedStats: computed('model.@each.date', function() {
return this.get('model').sortBy('date');
})
此外,余烬提供了排序宏:
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')
// ...
这篇关于灰烬-在findAll之后对模型进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!