EmberJS RESTSerializer与嵌入的hasMay关系

时间:2021-01-30 09:27:55

I have simple api that returns channels, and each channel contains a number of stories. The API returns the following structure for a channel:

我有简单的api返回频道,每个频道都包含许多故事。 API为通道返回以下结构:

{
    "id": 1,
    "name": "The Awesome Channel",
    "stories": [
        {
            "icon": null,
            "id": 3,
            "pub_date": "2015-08-08T17:32:00.000Z",
            "title": "First Cool Story"
        },
        {
            "icon": null,
            "id": 4,
            "pub_date": "2015-10-20T12:24:00.000Z",
            "title": "Another Cool Story"
        }
    ]
}

I have the two following models defined, channel.js:

我定义了以下两个模型,channel.js:

import DS from 'ember-data';

export default DS.Model.extend({
    name: DS.attr('string'),
    bgurl: DS.attr('string'),
    stories: DS.hasMany('story')
});

and story.js:

和story.js:

import DS from 'ember-data';

export default DS.Model.extend({
    channelId: DS.attr('number'),
    title: DS.attr('string'),
    pubDate: DS.attr('string'),
    icon: DS.attr('string'),
});

I also have this RESTSerializer to deserialize a channel:

我也有这个RESTSerializer来反序列化一个通道:

import DS from 'ember-data';

export default DS.RESTSerializer.extend({
    normalizeArrayResponse(store, primaryModelClass, hash, id, requestType) {
        var newHash = {
            channels: hash
        };
        return this._super(store, primaryModelClass, newHash, id, requestType);
    },

     normalizeSingleResponse(store, primaryModelClass, hash, id, requestType) {
        // Convert embedded data into a lost of story ids
        var stories = hash.stories.map(function(story) {
            return story.id;
        });
        delete hash.stories;
        hash.stories = stories;
        var newHash = {
            channel: hash,
        };
        return this._super(store, primaryModelClass, newHash, id, requestType);
    }
});

The code above works but it will make a new request to the server for each story in the channel, but since the data is already included in the response there is no need for those extra requests. If I leave the story data as-in then normalizing the data will fail.

上面的代码可以工作,但它会为通道中的每个故事向服务器发出新请求,但由于数据已包含在响应中,因此不需要这些额外请求。如果我将故事数据保留为原样,则规范化数据将失败。

Is there a way to indicated that the data for related models is embedded in the response?

有没有办法表明相关模型的数据嵌入在响应中?

1 个解决方案

#1


1  

Did you try to declare stories as embedded in your channel serializer ?

您是否尝试将故事声明为嵌入在频道序列化程序中?

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    stories: { embedded: 'always' }
  },

  normalizeArrayResponse(store, primaryModelClass, hash, id, requestType) {

  ...
});

#1


1  

Did you try to declare stories as embedded in your channel serializer ?

您是否尝试将故事声明为嵌入在频道序列化程序中?

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    stories: { embedded: 'always' }
  },

  normalizeArrayResponse(store, primaryModelClass, hash, id, requestType) {

  ...
});