访问emberjs模板中的关系字段?
Accessing relationship field in emberjs template?
这是我的 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
returns我一个承诺。我的问题是如何在模板端呈现 name
。
The team
with proper id was already populates with model api call as
relationship.
编辑
问题是 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
的结构有问题,但我不知道具体是什么?
显示的响应不标准 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"
}
}
}
}]
}
这是我的 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
returns我一个承诺。我的问题是如何在模板端呈现 name
。
The
team
with proper id was already populates with model api call as relationship.
编辑
问题是 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
的结构有问题,但我不知道具体是什么?
显示的响应不标准 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"
}
}
}
}]
}