我对 Ember 数据和 Mongodb 嵌入对象有疑问。这是我的模型:
App.Contact = App.Entity.extend({
name : DS.attr('string'),
firstname : DS.attr('string'),
additional_names : DS.attr('string'),
civility : DS.attr('string'),
birthday : DS.attr('date'),
organization : DS.belongsTo('App.Organization'),
role : DS.attr('string'),
photo_source : DS.attr('string'),
photo_uri : DS.attr('string'),
gravatar_mail : DS.attr('string'),
addresses : DS.hasMany('App.Address', { embedded: true }),
emails : DS.hasMany('App.Email', { embedded: true }),
phones : DS.hasMany('App.Phone', { embedded: true })
});
现在我正在通过 API 获取联系人: (GET /app/api/v1/contact/4f86c4774ab63c2417000001/) 这是我得到的:
{
"additional_names": null,
"addresses": [],
"birthday": null,
"civility": null,
"emails": [
{
"email": "alexandre@test.com",
"label": null,
"resource_uri": "/app/api/v1/contact/4f86c4774ab63c2417000001/emails/0/",
"type": "HOME"
}
],
"firstname": "Alexandre",
"gravatar_mail": null,
"groups": [],
"id": "4f86c4774ab63c2417000001",
"name": "Simoui",
"organization": null,
"phones": [],
"photo_source": null,
"photo_uri": "/static/img/nophoto.png",
"resource_uri": "/app/api/v1/contact/4f86c4774ab63c2417000001/",
"role": null
}
我的“根”对象有一个 id,但嵌入的对象“电子邮件”没有。因为在 mongodb 中,没有在子文档上设置 id,而只在根文档上设置。
这样 ember-data 会看到“电子邮件”对象没有 id,然后它会尝试通过 API 获取完整的对象。例如:GET /app/api/v1/email/set// 404(未找到)
为了确定这是一个赖特问题,我尝试返回带有假 ID 字段的 Mongodb 子文档。喜欢:(请参阅电子邮件对象的区别)
{
"additional_names": null,
"addresses": [],
"birthday": null,
"civility": null,
"emails": [
{
"id": 431,
"email": "alexandre@test.com",
"label": null,
"resource_uri": "/app/api/v1/contact/4f86c4774ab63c2417000001/emails/0/",
"type": "HOME"
}
],
"firstname": "Alexandre",
"gravatar_mail": null,
"groups": [],
"id": "4f86c4774ab63c2417000001",
"name": "Simoui",
"organization": null,
"phones": [],
"photo_source": null,
"photo_uri": "/static/img/nophoto.png",
"resource_uri": "/app/api/v1/contact/4f86c4774ab63c2417000001/",
"role": null
}
然后我没有问题一切都很好。所以我的问题是:有没有办法解决它?