查看 Ember 页面上的指南,我无法弄清楚如何在一对多的关系中连接模型。
App.Post = DS.Model.extend({
comments: DS.hasMany('comment')
});
App.Comment = DS.Model.extend({
post: DS.belongsTo('post')
});
1. 如何定义夹具?A 或 B 或其他
A)每个帖子“对象”中的评论
App.Post.FIXTURES = [
{
id:1,
title:"hello world",
body:"hey ho",
comments:[
{
text: "Very nice"
},
{
text: "Very nice indeed"
},
]
},
{
id:2,
title:"hello again",
body:"I'm Bob",
comments:[{
text: "Pretty cool actually"
}]
}
]
B)单独评论并与ID链接到帖子
App.Post.FIXTURES = [
{
id:1,
title:"hello world",
body:"hey ho"
},
{
id:2,
title:"hello again",
body:"I'm Bob"
}
]
App.Comment.FIXTURES = [
{
post_id:1,
text: "Very nice"
},
{
post_id:1,
text: "Very nice indeed"
},
{
post_id:2,
text: "Pretty cool actually"
}
]
2.关于从服务器获取
A)我是否需要单独加载帖子和评论,或者一次调用全部加载,使其结构类似于 1A 案例?
B)如果我想单独加载它们怎么办,例如等待用户单击确定评论链接,除非用户请求,否则无需为页面上的每个博客帖子下载 1000 条评论。
您能否提供一个简单版本的每个调用的样子?