简体   繁体   English

Ember-data 2:如何在不存储的情况下从服务器获取记录来检索belongsTo模型ID

[英]Ember-data 2: How to retrieve belongsTo model id without store fetching the record from the server

I have an activity model that belongs to a report . 我有一个属于reportactivity模型。 I have a scenario where I have only the activity records and all I want to do is get the report id, without fetching the whole record. 我有一个场景,其中我只有活动记录,而我要做的就是获取report ID,而不获取整个记录。

activity.get('report_id') // undefined

activity.get('report.id') // the id! But after a full fetch

Obviously, it knows the id, but I can't figure out the right incantation. 显然,它知道ID,但是我无法弄清楚正确的咒语。 Anyone have any thoughts? 有人有什么想法吗?

ps. ps。 It looks like this changed in ember-data 2 so a previous answer will not work. 看起来在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中列出的功能。

Though locks answer was very helpful in understanding the direction this is going, it's not immediately practical. 尽管锁定答案对于理解此事的发展方向非常有帮助,但并非立即可行。 The RFC have a comment with a simple solution for right now. RFC现在有一个简单解决方案的注释。

  1. Add a new id attribute to the model 向模型添加新的id属性
  2. Populate in the serializer 在序列化器中填充

https://github.com/emberjs/rfcs/pull/57#issuecomment-121008369 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM