我正在尝试使用 ember-data 的 Ember 应用程序和使用 Sequelize.js 提供来自 MySQL 的数据的 Node.js 后端。
我的问题:
如果我有一个通过 hasMany 与 Post 模型关联的 Comment 模型,则 ember-data 的预期 JSON 看起来像
{
"post": {
"comment_ids": [1, 2, 3]
}
}
使用sequelize 查询/生成此JSON 而无需昂贵的循环等的最佳方法是什么?
Comment 模型有一个带有 post_id 的外键。
我去实现它是这样的:
// include comments
Post.all({
where: "..",
include: [Comment]
}).success(function(posts) {
// collect IDs
_.each(posts, function(element) {
element["comments"] = _.pluck(element.comments, 'id');
});
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)