Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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

@pixelhandler

This comment has been minimized.

Show comment
Hide comment
@pixelhandler

pixelhandler May 1, 2014

In the attrs prop the options for embedded records using serialize and deserialize (extract) make sense with a configurable choice of records or ids. Also It would be good for backwards compatibility to support embedded: 'always' as shorthand syntax for {serialize: 'records', deserialize: 'records'}. I'm going to add these options in the DS.EmbeddedRecordsMixin on PR #1637

pixelhandler commented May 1, 2014

In the attrs prop the options for embedded records using serialize and deserialize (extract) make sense with a configurable choice of records or ids. Also It would be good for backwards compatibility to support embedded: 'always' as shorthand syntax for {serialize: 'records', deserialize: 'records'}. I'm going to add these options in the DS.EmbeddedRecordsMixin on PR #1637

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment