我有两种模式:
票:
export default DS.Model.extend({
name: DS.attr('string'),
email: DS.attr('string'),
messages: DS.hasMany('message'),
})
消息:
export default DS.Model.extend({
message: DS.attr('string'),
date: DS.attr('date'),
ticket: DS.belongsTo('ticket', { async: true }),
});
使用此模型方法的路由器:
model(params) {
return this.get('store').findRecord('ticket', params.id, {reload: true, include: 'messages'});
},
在页面重新加载一切正常时,只调用了tickets/:id
但有时ember会对messages/:id
进行一堆调用。为什么不使用包含的数据并尝试从服务器检索?
我在hasMany关系上尝试了async: false
,但我发现了这个错误:
Error: Assertion Failed: You looked up the 'messages' relationship on a 'ticket' with id 15 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (`DS.hasMany({ async: true })`)
有什么想法吗?
这是GET tickets/:id
回复:
{"data":
{
"id":"15",
"type":"tickets",
"attributes":{...},
"relationships":{"messages":{"data":[{"id":"1478482584658","type":"messages"},{"id":"1478482588516","type":"messages"},{"id":"1478517720","type":"messages"},{"id":"1478517813","type":"messages"},{"id":"1478517893","type":"messages"},{"id":"1478530030","type":"messages"},{"id":"1478530032","type":"messages"},{"id":"1478533446","type":"messages"}]}}},
"included":[
{"id":"1478482584658","type":"messages","attributes":{...}},{"id":"1478482588516","type":"messages","attributes":{...}},
...
}
}
]
}
为什么不从included
获取数据并对服务器进行其他调用?
答案 0 :(得分:0)
承诺怎么样?
在处理人际关系时,重要的是要记住他们会回报承诺。 https://guides.emberjs.com/v2.1.0/models/working-with-relationships/#toc_relationships-as-promises