[英]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.