考虑这种关系:
Office ->hasMany rooms -> hasMany tables -> hasMany drawers
以JSONAPI格式表示如下:
考虑以下关系:
官方 - >拥有多个房间 - > hasMany表 - >有很多抽屉
由以下JSON代表:
"data": [{
"type": "office",
"id": 201,
"attributes": {
"owner": 5,
......
"total-rooms": 1,
},
"links": {
"self": "/offices/201",
},
"relationships": {
"rooms": {
"links": {
"self": "/offices/201/relationships/rooms",
"related": "/offices/201/rooms"
},
"data":[
{ "type": "room", "id": 201 },
]
}
}
}],
"included": [ {
"type":"room",
"id": 201,
"attributes": {
"office": 5,
"some-property": "Test Property tpo see if it works"
},
"links": {
"self": "/offices/5/rooms/201",
},
"relationships": {
"tables": {
"links": {
"self": "/offices/5/rooms/201/relationships/rooms",
"related": "/offices/5/rooms/201/tables",
},
"data":[
{ "type": "tables", "id": 801 },
]
}
}
},{
"type":"tables",
"id": 801,
"attributes": {
"room": 201,
"table-test-data":"Some test data",
},
"links": {
"self": "/offices/5/rooms/201/tables/801",
},
"relationships": {
"drawers": {
"links": {
"self": "/offices/5/rooms/201/tables/810/relationships/drawers",
"related": "/properties/5/rooms/201/tables/810/drawers",
},
"data":[
{ "type": "drawer", "id": 1001 },
]
}
}
},
]
}
我尝试使用以下型号在EmberJS / Ember数据中对它们进行建模:
//Office
export default DS.Model.extend({
totalRooms: DS.attr('number');
rooms: DS.hasMany('room');
});
//Rooms
export default DS.Model.extend({
total: DS.attr('number');
tables: DS.hasMany('table');
});
//Table
export default DS.Model.extend({
totalTables: DS.attr('number');
drawers: DS.hasMany('drawer');
});
//Drawer
export default DS.Model.extend({
totalDrawers: DS.attr('number');
drawers: DS.attr('string')
});
但是,Ember数据仅加载主数据的第一级关系。也就是说,在我的控制器中,当我执行以下操作时:
model.offices.forEach(function(office) {
office.rooms.forEach(function(room){
//Can access room properties
// cannot access tables
console.log(room.tables); //prints a class instead of object
})
})
我可以访问房间的所有数据,但是当我尝试更深入一级时,例如表格是未定义的。我不想使用多个调用来加载数据,并且想知道我是否遗漏了服务器返回的JSON中的内容,或者我需要以不同的方式处理它?</ p>
非常感谢任何帮助。