Ember-data 嵌入对象存储为单独的对象 [英] Ember-data embedded objects stored as separate objects
问题描述
我想知道是否可以定义一个存储到另一个模型中的模型.
I was wondering if it would be possible to define a model stored into another one.
我有这种结构:
Model Contact
String name
Model Address (hasMany)
Model Phone (hasMany)
在我的后端,地址和电话是包含在联系人"文档中的 MongoDB 嵌入文档.
On my backend, addresses and phones are MongoDB's embedded documents contained in a "Contact" document.
而且,只要它们是嵌入式文档,它们就没有 id.当我在 Emberjs/data 层时,它们很好地加载了嵌入式选项(参见 https://github.com/emberjs/data#one-to-one 部分)但存储为单独的对象,这会导致更新或保存时出现问题...
And, as long as they are embedded documents, they do not have an id. And when I am on the Emberjs/data layer, they are -well- loaded with the embedded option (cf. the end of the https://github.com/emberjs/data#one-to-one section) but stored as separate objects and this cause trouble when updating or saving...
推荐答案
您正在使用 RESTadapter...保存时要序列化所有嵌入的关系吗?
You are using the RESTadapter... when you are saving you want to serialize all the relationships embedded?
当您保存或更新您的记录时,使用
When you are saving or updating your record pass in the options hash to the toJSON method with
{associations: true}
查看 ember-data 的单元测试示例:https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/to_json_test.js
Take a look at the unit tests on ember-data for examples: https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/to_json_test.js
deepEqual(record.toJSON({ associations: true }),
{ id: 1, name: "Chad", phone_numbers: [{
id: 7,
number: '123'
},
{
id: 8,
number: '345'
},
{
id: 9,
number: '789'
}
]},
"association is updated after editing associations array");
});
希望这有帮助..
这篇关于Ember-data 嵌入对象存储为单独的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!