Embedded Ember data objects stored as separate objects

I was wondering if it is possible to define a model stored in another.

I have this structure:

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

My server, addresses, and phones contain embedded MongoDB documents contained in the Contact document.

And, while they are embedded documents, they do not have an identifier. And when I'm at the Emberjs / data level, they are loaded with a built-in option (see the End of the https://github.com/emberjs/data#one-to-one section), but they are saved as separate objects, and this causes problems when updating or saving ...

+1
source share
1 answer

You are using a RESTadapter ... When you save, do you want to serialize all the nested relationships?

When you save or update your write skip in the hash setting of 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.

+3
source

All Articles