Ember Store:排序,过滤,查找记录

Mat*_*att 2 findby ember.js ember-data

我在索引路径中将记录加载到我的商店:

model: function(){
    return Ember.RSVP.hash({
        cars: this.store.query('car',{}).then(function(data){

        })
    });
}
Run Code Online (Sandbox Code Playgroud)

然后我去我的汽车路线peekAll(没有网络请求)并获得所有的汽车记录:

model: function() {
    return Ember.RSVP.hash({
        cars: this.store.peekAll('car').sortBy('name')
    });
}
Run Code Online (Sandbox Code Playgroud)

您会注意到我可以使用'sortBy'根据本地存储数据库中的字段对记录进行排序.

我不明白的是如何从商店过滤或查找记录?例如,如果我想执行以下操作该怎么办:

  • 按名称对所有车辆记录进行排序,然后过滤/查找车辆,使其仅返回1998年或之后购买的车辆
  • 过滤记录,所以我只显示'Jaguars'的汽车
  • 即使商店有超过10条记录,也只返回最多10条记录.

我已经查看了findByfilterBy,但是文档似乎有点缺乏有关如何使用示例实现的细节.

Lux*_*Lux 5

首先,如果你传递{}.query()你可能应该使用findAll.

你可以使用filter:

this.store.peekAll('car')
  .filter(car => car.get('year') >= 1998)
  .sortBy('name')
  .slice(0, 10);
Run Code Online (Sandbox Code Playgroud)

filterBy是否要过滤特定值.所以,如果你想要所有caryear 1998:

this.store.peekAll('car')
  .filterBy('year', '1998');
Run Code Online (Sandbox Code Playgroud)