Created
August 5, 2011 17:10
-
-
Save jumski/1128000 to your computer and use it in GitHub Desktop.
BackboneJS sync hack to namespace params when saving/updating model
This file contains 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
### | |
Usage: | |
* add model.name property that will be used as a namespace in the json request | |
* put this code before your Backbone app code | |
* use toJSON() as usual (so there is no namespacing in your templates) | |
* your model's data will be sent under model.name key when calling save() | |
### | |
# save reference to Backbone.sync | |
Backbone.oldSync = Backbone.sync | |
# override original method | |
Backbone.sync = (method, model, options) -> | |
# save reference to original toJSON() | |
model.oldToJSON = model.toJSON | |
# override this model instance toJSON() method | |
model.toJSON = -> | |
json = {} | |
# namespace original json values under model.name key | |
json[model.name] = @oldToJSON() | |
json | |
# call original sync method | |
syncReturnValue = Backbone.oldSync method, model, options | |
# restore original toJSON() on this model instance | |
model.toJSON = model.oldToJSON | |
delete model.oldToJSON | |
# return value returned by original sync | |
syncReturnValue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seems like a little less trickier than this over here https://gist.github.com/719080