我有这个问题模型:
App.Question = DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string'),
subjects: DS.hasMany('subject', {embedded: 'always')
});
App.Subject = DS.Model.extend({
description: DS.attr('string')
question: DS.belongsTo('question')
});
App.QuestionAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:3000'
});
App.QuestionSerializer = DS.RESTSerializer.extend({
primaryKey: '_id',
serializeId: function(id) {
return id.toString();
}
});
这是我的数据样本:
{
"questions":
[{
"_OwnerId": "53e440b3b6d1de0b3cc12b44",
"__v": 0,
"_id": "53f03ac07295ef2b467551d9",
"description": "0987654321",
"subjects": [{"description":"cs", "question":"53f03ac07295ef2b467551d9" },
{"description":"physics","question":"53f03ac07295ef2b467551d9" }],
"title": "2134567890"
}]
}
我的所有代码都没有主题区域。我相信我必须覆盖addhasmany或serializeHasMany,其中我不知道两者之间的差异。但是我设置JSON的方式是否正确,以及如何编写序列化器来正确序列化主题区域?
谢谢,
答案 0 :(得分:1)
您可以使用EmbeddedRecordsMixin http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html,您可以轻松配置它以序列化和反序列化嵌入式关系。查看第一个示例中的'embedded' : 'always'
选项