Skip to content

Instantly share code, notes, and snippets.

@igorT
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorT/ec4d5eadefe08fa90274 to your computer and use it in GitHub Desktop.
Save igorT/ec4d5eadefe08fa90274 to your computer and use it in GitHub Desktop.

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
Copy link

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