![](/img/trans.png)
[英]Ember-data get “belongsTo” attr (id) from model without resolving the actual object
[英]Ember-data 2: How to retrieve belongsTo model id without store fetching the record from the server
我有一個屬於report
的activity
模型。 我有一個場景,其中我只有活動記錄,而我要做的就是獲取report
ID,而不獲取整個記錄。
activity.get('report_id') // undefined
activity.get('report.id') // the id! But after a full fetch
顯然,它知道ID,但是我無法弄清楚正確的咒語。 有人有什么想法嗎?
ps。 看起來在ember-data 2中已更改,因此先前的答案將無效。
-------------------------------
Ember : 2.4.1
Ember Data : 2.4.0+9f8c40927a
jQuery : 1.11.3
Ember Simple Auth : 1.0.0
-------------------------------
我不認為在Release通道中可以做到這一點,但是如果您使用Canary並啟用ds-references
功能標記 ,則可以使用RFC 57中列出的功能。
盡管鎖定答案對於理解此事的發展方向非常有幫助,但並非立即可行。 RFC現在有一個簡單解決方案的注釋。
https://github.com/emberjs/rfcs/pull/57#issuecomment-121008369
//models/report-activity.js
export default DS.Model.extend({
report: belongsTo('report', {async: true}),
reportId: attr('string'), //set in serializer
});
//serializers/report-activiy.js
export default DS.ActiveModelSerializer.extend({
normalizeAttributes(typeClass, hash) {
if (hash.report_id) {
hash.reportId = hash.report_id;
}
return this._super(typeClass, hash);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.