![](/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.