在同一个 api 调用 emberjs 中加载多个模型数据?

Load multiple model data in same api call emberjs?

所以这是我在 emberjs

中定义的两个模型

match.js

import DS from 'ember-data';

export default DS.Model.extend({
  team: DS.belongsTo('team', {async:true}),
  opponent: DS.belongsTo('team', {async: true}),
  type: DS.attr('string'),
  squad: DS.attr('boolean')
});

team.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  logo: DS.attr('string')
});

我已经将比赛作为模型加载。在同一个 api 调用中,我还想为团队加载模型数据。 api 目前为止我的回复是

{
  "meta":{
    "type":"match"
  },
  "data":[
    {
      "id":1119536,
      "type":"match",
      "attributes":{
        "id":1119536,
        "team":{
          "type":"team",
          "id":1,
          "attributes":{
            "id":1,
            "name":"England",
            "logo":null
          }
        },
        "opponent":{
          "type":"team",
          "id":3,
          "attributes":{
            "id":3,
            "name":"Pakistan",
            "logo":null
          }
        }
      }
    }
  ]
}

match 模型数据已正确加载,但我遇到了与 team 数据相同的问题。响应来自浏览器中的网络,我已经使用浏览器上的 ember 插件检查了模型,团队数据未加载。我如何使用相同的 api 调用来加载多个模型。

需要注意的几点:

  • 不要将 id 放入 attributes
  • 不要命名属性 type。真的不要!这是一个保留关键字。
  • 关系不是属性,应该在relationships
  • 使用 included 数组侧载数据
  • ids 必须是字符串

例如,这将是一个有效的负载:

{
  "meta": {
    "type": "match"
  },
  "data": [
    {
      "id": "1119536",
      "type": "team",
      "attributes": {
        "match-type": "match"
      },
      "relationships": {
        "team": {
          "data": {
            "type": "team",
            "id": "1"
          }
        },
        "opponent": {
          "data": {
            "type": "team",
            "id": "3"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "team",
      "id": "1",
      "attributes": {
        "name": "England",
        "logo": null
      }
    },
    {
      "type": "team",
      "id": "3",
      "attributes": {
        "name": "Pakistan",
        "logo": null
      }
    }
  ]
}