Created
December 18, 2014 13:58
-
-
Save pads/f2861fe6ea21f67f4ac7 to your computer and use it in GitHub Desktop.
Ember Recursive Model Rollback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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