Skip to content

Instantly share code, notes, and snippets.

@pads
Created December 18, 2014 13:58
Show Gist options
  • Save pads/f2861fe6ea21f67f4ac7 to your computer and use it in GitHub Desktop.
Save pads/f2861fe6ea21f67f4ac7 to your computer and use it in GitHub Desktop.
Ember Recursive Model Rollback
# Ember data does not dirty the parent model if any of it's children change so
# no rollback can occur for a child. This fixes this by resetting a child's
# data to the internal data tracked by the parent model.
DS.Model.reopen
rollbackBelongsTo: (relationshipName) ->
thisName = @constructor.typeKey
relationshipInstance = @get relationshipName
# Rollback any attributes that were changed in the relationship
relationshipInstance.rollback()
# Recursively follow the relationships and roll them back too.
relationshipInstance.constructor.eachRelationship (name, childRelationship) ->
unless name is thisName
childRelationshipInstance = relationshipInstance.get name
if childRelationshipInstance and childRelationship.kind is 'belongsTo'
relationshipInstance.rollbackBelongsTo name
# Rollback the relationship instance back to the previous set one
@set relationshipName, @_data[relationshipName]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment