Skip to content

Instantly share code, notes, and snippets.

@shaunc
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaunc/9699236 to your computer and use it in GitHub Desktop.
Save shaunc/9699236 to your computer and use it in GitHub Desktop.
Sketch of proposal for Ember.JSONSerializer.serializeBelongsTo, handling async and polymorphic belongsTo, hasMany
# The idea is that (even for an async relationship) we have available either
# the related object, or the as-of-yet not unserialized reference to that
# object. In the latter case, we can use directly.
#
# This avoids possible generation of more server requests for related
# objects while saving a given object.
#
# Issues:
#
# 1) Potentially, the not-unserialized reference could be in a different
# format. (E.g. we are acting as a transponder...). AFAIK it is acceptable
# to require customization in this case.
#
# 2) Uses duck-typing / internals to get required objects. Is there a better
# way to detect state and retrieve related object or reference?
#
#
DS.JSONSerializer.reopen
# returns object related by given relationship.
#
# The form can be related object, as-yet not unserialized
# reference, or null
getRelated: ( record, key ) ->
related = Ember.get record, key
if related?.then?
if !related.isFulfilled
related = record.get( 'data' )[ key ]
else
related = related.content
return related
getRelatedId: ( related ) ->
return related.id || related
serializeBelongsTo: (record, json, relationship) ->
key = relationship.key
belongsTo = @getRelated record, key
key = @keyForRelationship?( key ) || key
if Ember.isNone(belongsTo)
json[key] = belongsTo
else
json[key] = @getRelatedId belongsTo
if relationship.options.polymorphic
this.serializePolymorphicType(record, json, relationship)
serializeHasMany: (record, json, relationship) ->
key = relationship.key
relationshipType = DS.RelationshipChange.determineRelationshipType(
record.constructor, relationship)
# for opposite of one-to-many, default is to only save opposite
if relationshipType is "manyToNone" or relationshipType is "manyToMany"
related = @getRelated(record, key)
if Ember.isNone(related)
json[key]= related
else
json[key] = related.mapBy("id")
if relationship.options.polymorphic
this.serializePolymorphicType(record, json, relationship)
return
serializePolymorphicType: (record, json, relationship) ->
key = relationship.key
kind = relationship.kind
convert = (related)=>
if !Ember.isNone(related)
typeKey = related.constructor.typeKey || related.type
return {
id: @getRelatedId related
type: typeKey
}
else
related
related = @getRelated( record, key )
key = @keyForAttribute?(key) || key
if kind == 'belongsTo'
json[key] = convert( related )
else if (kind == 'hasMany' )
json[key] = related.content.map (rel)->
convert(rel)
else
Ember.assert(
'Relationship kind {#relationship.kind} not supported yet', false )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment