Ember数据嵌入对象存储为单独的对象 [英] Ember-data embedded objects stored as separate objects

查看:90
本文介绍了Ember数据嵌入对象存储为单独的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这样一种结构:

 模型联系
字符串名称
模型地址(hasMany)
模型电话(hasMany)

在我的后端,地址和电话是MongoDB的联系人文档中包含的嵌入式文档。



而且,只要它们是嵌入式文档,它们就没有id。
当我在Emberjs /数据层时,它们将被嵌入的选项(参见 https://github.com/emberjs/data#one-tooneone部分),但存储为单独的对象,这会在更新或保存时造成麻烦

您正在使用RESTadapter ...
,当您保存时,您希望序列化所有嵌入的关系?



当您使用

{associations:true}

查看ember的单元测试例如:
https://github.com/emberjs/data/blob/master/packages/ember-data/tests/unit/to_json_test.js

  deepEqual(record.toJ SON({associations:true}),
{id:1,name:Chad,phone_numbers:[{
id:7,
number:'123'
} ,
{
id:8,
number:'345'
},
{
id:9,
number:'789 '
}
]},
关联在编辑关联数组后被更新);
});

希望这有帮助..


I was wondering if it would be possible to define a model stored into another one.

I have this kind of structure:

Model Contact
    String name
    Model Address (hasMany)
    Model Phone (hasMany)

On my backend, addresses and phones are MongoDB's embedded documents contained in a "Contact" document.

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...

解决方案

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}

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");
});

Hope this helps..

这篇关于Ember数据嵌入对象存储为单独的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆