我们有一个简单的一对多关系(不要注意破碎的多元化):
QuickOrder = DS.Model.extend({
quickOrderAddresss: DS.hasMany('quickOrderAddress', {
async: true
})
});
QuickOrderAddress = DS.Model.extend({
quickOrder: DS.belongsTo('quickOrder'),
...
state: DS.attr('string'),
city: DS.attr('string'),
street: DS.attr('string')
...
});
由于服务器使用了非常恶心的JSON格式,我们实现了自己的适配器,能够按ID搜索订单:
QuickOrderAdapter = DS.Adapter.extend({
find: function(store, type, id) {
.........
return new Ember.RSVP.Promise(function(resolve, reject) {
jQuery.getJSON(addressesUrl).then(function(data) {
//lots of ugly stuff here
Ember.run(null, resolve, result);
}, function(jqXHR) {
jqXHR.then = null; // tame jQuery's ill mannered promises
Ember.run(null, reject, jqXHR);
});
});
}
});
现在我们要删除async: true
并使用async: false
代替。如果加载是同步的,你能给我一个例子来说明包含数据的JSON格式吗?
答案 0 :(得分:1)
答案在Ember指南(http://emberjs.com/guides/models/the-rest-adapter/#toc_sideloaded-relationships)中。
类似的东西:
{
"quickOrder": {
"id": 1,
"quickOrderAddresss": [1,2]
},
"quickOrderAddress": [{
"id": 1,
"quickOrder": 1,
//blah blah more stuff
},{
"id": 2,
"quickOrder": 1,
//blah blah more stuff
},
}