我在我的应用程序中使用 Emberfire,我正在尝试 findAll
统计信息,然后按键对该模型进行排序,如下例所示。但是,当我以这种方式排序时,我无法在模板中查看实时更新,我必须重新加载页面才能在 View 中查看新的/更新的数据。
model() {
return this.store
.findAll('stats', {
reload: true,
backgroundReload: true
})
.then(stats => stats.sortBy('date'));
}
最佳答案
您必须在您的 Controller 或组件中定义一个计算属性,该属性返回排序后的统计信息。不要在路由的模型 Hook 处对数据进行排序。只需返回 findAll 的 promise 即可。
例如:
//controller.js or component.js
sortedStats: computed('model.@each.date', function() {
return this.get('model').sortBy('date');
})
此外,ember 还提供了一个 sort macro :
import { sort } from '@ember/object/computed';
使用它可以更优雅的解决你的需求:
// ...
this.init() {
this._super(...arguments);
this.set('sortDefinition', ['date:asc']);
}
sortedStats: sort('model.@each.date', 'sortDefinition')
// ...
关于ember.js - Ember - 在 findAll 之后对模型进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53489283/