Created
July 17, 2011 17:56
-
-
Save craigspaeth/1087868 to your computer and use it in GitHub Desktop.
Coffescript override Backbone.sync to be Ruby on Rails friendly
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
# Override Backbone.sync to be rails friendly | |
Backbone.sync = ((original) => (method, model, options) -> | |
options ?= {} | |
# Wrap model's attrs in a Rails friendly way by injecting it into options.data | |
if not options.data? and model? and (method is "create" or method is "update" or method is "delete") | |
unless model.modelName? | |
throw new Error "You must specify a modelName in a Rails friendly | |
underscore format for this model to be persisted" | |
options.contentType = 'application/json' | |
options.data = id: model.get('id') | |
options.data[model.modelName] = model.toJSON() | |
delete options.data[model.modelName].id | |
options.data = JSON.stringify options.data | |
original method, model, options | |
)(Backbone.sync) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment