问题
I have some data models that look like this:
App.Order = DS.Model.extend({
name: DS.attr('string'),
legs: DS.hasMany('leg', {async: false})
});
App.Leg = DS.Model.extend({
name: DS.attr('string'),
order: DS.belongsTo('order'),
stops: DS.hasMany('stop', {async: false})
});
App.Stop = DS.Model.extend({
name: DS.attr('string'),
leg: DS.belongsTo('leg'),
});
The returned JSON looks like this:
{
"orders": [
{
"id": 1,
"name": "Order 1",
"legs": []
},
{
"id": 2,
"name": "Order 2",
"legs": [1]
},
{
"id": 3,
"name": "Order 3",
"legs": [2,3]
}
],
"legs": [
{
"id": 1,
"name": "Leg 1",
"stops": [1,2]
},
{
"id": 2,
"name": "Leg 2",
"stops": [2,3]
},
{
"id": 3,
"name": "Leg 3",
"stops": [1,3]
}
],
"stops": [
{
"id": 2,
"name": "Stop 2"
},
{
"id": 1,
"name": "Stop 1"
},
{
"id": 3,
"name": "Stop 3"
}
]
}
When I was using async:true
, ember made http requests for the related legs (legs?ids[]=1
and legs?ids[]=2&ids[]=3
) but would never make a request for the related stops.
So I switched to sideloading as shown above, but the stops are still NOT available from the related legs.
Can you help me understand how the loading of Models with nested hasMany relationships SHOULD be done? Or where I've gone wrong? I'm open to using either async: true
or async: false
, as long as I can get the nested data loaded in to the model.
来源:https://stackoverflow.com/questions/23748760/how-to-use-ember-data-with-nested-hasmany-relationships