Rationale: Currently it is easy(supposed to be) to load and save embedded records with ids. For example if you get a payload like
{ post: {
id:1,
comments:[{ id:1, content: 'hello'}, {id:2, content:'goodbye'}]
}}
you can just say
App.PostSerializer = App.ApplicationSerializer.extend DS.EmbeddedRecordsMixin
attrs: {
comments: {embedded: 'always'}
}
However this approach conflates embedding when loading and embedding when serializing. People often strugle with embedding only ids for a hasMany. Currently the suggested approach is to overwrite the serializeHasMany hook, but that is cumbersome and sometimes non trivial.
I think we should split the embedded option key into two, one for serializing and one for deserializing. (We might still keep but deprecate embedded: 'always' for backwards compatibility)
So you would have something like
App.PostSerializer = App.ApplicationSerializer.extend
attrs: {
comments: {serialize: 'records', deserialize: 'records'}
}
and you can easily then specify for cases where the embedded mixin is an overkill(90% of complexitiy of embeddnes is in the deserialize case)
App.PostSerializer = App.ApplicationSerializer.extend
attrs: {
comments: {serialize: 'ids'}
}
This way the EmbeddedMixin would still deal with loading complexitiy of embeddnes, and JSON/Rest serializer get nice options for nicely serializing relationships
In the
attrs
prop the options for embedded records usingserialize
anddeserialize
(extract) make sense with a configurable choice ofrecords
orids
. Also It would be good for backwards compatibility to supportembedded: 'always'
as shorthand syntax for{serialize: 'records', deserialize: 'records'}
. I'm going to add these options in the DS.EmbeddedRecordsMixin on PR #1637