[英]Ember-data 2: How to retrieve belongsTo model id without store fetching the record from the server
[英]Ember-data get “belongsTo” attr (id) from model without resolving the actual object
我想获取模型属性(id)“ belongsTo”,而无需强制服务器调用或解决该对象。 我有一个拥有用户的会员资格。 最多的用户已加载,因此我只想为其中一些用户进行服务器调用。 我最近更新为ember-data 1.13.4,而我的旧版本不再起作用。
会员资格:
user: DS.belongsTo('user', { async: false }),
到目前为止,我这样做是为了检查用户是否已经在商店中:
if (this.store.peekRecord('user', parseInt(membership._internalModel._data.user)) === null)
return false;
但是随着新的Ember-Data更新,_data不再包含belongsTo-Ids。
最终完成:
Error while processing route: workgroups Assertion Failed: You looked up the 'user' relationship on a 'membership' with id 21 but some of the associated records were not loaded.
我真的很感谢在此方面的所有帮助。
谢谢
找到了。 基于此答案:
ember-data:如何判断模型的async:真实关系是否已加载而不触发加载?
我能够找出余烬数据1.13.4解决方案:
membership._internalModel._relationships.initializedRelationships.user.canonicalState.id
这很丑。 还有更好的主意吗?
编辑:这可以用于扩展具有功能的DS.Model:
import DS from 'ember-data';
export default {
name: 'model-getid',
initialize: function() {
DS.Model.reopen({
getId: function(key) {
// TODO(sn): support hasMany as well
const rel = this._internalModel._relationships.initializedRelationships;
return rel && rel[key] && rel[key].canonicalState && rel[key].canonicalState.id;
}
});
}
};
现在我们可以在单一关系上做到这一点:
membership.getId('organisation')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.