访问emberjs模板中的关系字段?

时间:2018-06-05 16:53:42

标签: ember.js ember-data

以下是我emberjs模型

上的当前结构
import DS from 'ember-data';

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

我正在调用它的模板如下

  <div class="container">
    {{#each model as |match|}}
      <div class="match">
        <code>Match type : {{match.type}}</code>
        <p>Team 1 : {{match.team.name}}</p>
      </div>
    {{/each}}
  </div>

现在match.team给我一个承诺。我的问题是如何在模板端渲染name

  

具有正确ID的team已填充模型api调用为   关系。

修改

问题在于Promise解决了没有内容。以下是json响应

{
  "meta": {
    "type": "match"
  },
  "included": [{
    "type": "team",
    "id": 3,
    "attributes": {
      "id": 3,
      "name": "teamName",
      "logo": null,
      "created-at": "2018-06-05T07:05:42.000Z",
      "updated-at": "2018-06-05T07:05:42.000Z"
    }
  }],
  "data": [{
    "id": 1124639,
    "type": "match",
    "attributes": {
      "id": 1124639,
      "team": 77,
      "opponent": 1,
      "starts-on": "2018-06-10T00:00:00.000Z",
      "created-at": "2018-06-05T08:30:13.000Z",
      "updated-at": "2018-06-05T08:30:13.000Z",
      "relationships": {
        "team": {
          "data": {
            "id": 77,
            "name": "teamName",
            "logo": null,
            "created-at": "2018-06-05T07:05:57.000Z",
            "updated-at": "2018-06-05T07:05:57.000Z",
            "type": "team"
          }
        }
      }
    }
  }]
}

我假设relationships的结构有问题,但无法准确了解究竟是什么?

1 个答案:

答案 0 :(得分:3)

显示的共鸣不是标准的JSON-API。你想要的东西看起来像这样:

{
  "meta": {
    "type": "match"
  },
  "included": [{
    "type": "team",
    "id": 77,
    "attributes": {
      "id": 77,
      "name": "teamName",
      "logo": null,
      "created-at": "2018-06-05T07:05:42.000Z",
      "updated-at": "2018-06-05T07:05:42.000Z"
    }
  }],
  "data": [{
    "id": 1124639,
    "type": "match",
    "attributes": {
      "id": 1124639,
      "opponent": 1,
      "starts-on": "2018-06-10T00:00:00.000Z",
      "created-at": "2018-06-05T08:30:13.000Z",
      "updated-at": "2018-06-05T08:30:13.000Z"
    },
    "relationships": {
      "team": {
        "data": {
          "id": 77,
          "type": "team"
        }
      }
    }
  }]
}