使用Ember Inspector,我可以很好地看到所有记录都位于缓存中。使用peekRecord
我可以从缓存中获取记录。如果缓存中存在上一条/下一条记录id
,而没有向服务器发出请求,是否有一种优雅的方式可以获得上一条/下一条记录?{/ p>
否则我需要做这样的事情:
model(params) {
return {
item : this.get('store').findRecord('item', params.item_id, {reload: false, backgroundReload: false}),
currId : params.item_id,
prevId : this.get('store').peekAll('item').reduce((previousValue, item, index, enumerable) => {
var found = found || false;
if (item.id == params.item_id) found = true;
if (found) return previousValue;
else return item.id;
}, false),
nextId : this.get('store').peekAll('item').reduce((previousValue, item, index, enumerable) => {
var found = found || false;
if (found === 2) return previousValue;
if (found === 1) found = 2;
if (found === true) found = 1;
if (item.id == params.item_id) found = true;
return item.id;
}, false)
};
}
(这是一个存根。我知道nextId
仅在查询后至少有两条记录时才有效。)
不优雅,头重脚轻(没有休息),没有验证(然而,在我继续沿着这条路走下去之前)。