如何在 ember 中使用名称而不是使用 id 查看记录

How to peek a record using name rather then using id in ember

所以在 rails 中我们可以通过名称、id 等找到记录,类似地我想在 ember 中做而无需发出服务器请求 我有一个名为 person{id, name} 的模型。如果我想通过 id 查看记录,我会这样做:

this.get('store').peekRecord('person', id)

这给了我基于 id 的记录,但现在我想查看具有特定名称的记录,我尝试了这样的事情:

this.get('store').peekRecord('person', {name: "testname"}) 

哪个剂量似乎不起作用。 我需要一种仅使用名称

来查看记录的方法

您只能使用模型的唯一标识符进行 peekRecord,即 id 属性。如果您确实想提出请求,那么 queryRecord 就是您想要的。但你不想发出请求,所以唯一的选择是 peekAll 并过滤结果,

let allRecord = this.get('store').peekAll('person');
let filteredResult = allRecord.filterBy('name','testname');
//filteredResult is an array of matching records