所以这是我在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调用来加载多个模型。
答案 0 :(得分:4)
要注意的一些事项:
id
放入attributes
type
。真的不!这是一个保留的关键字。relationships
included
数组来加载数据所以例如这将是一个有效的有效载荷:
{
"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
}
}
]
}