Skip to content

Instantly share code, notes, and snippets.

@craigspaeth
Created July 17, 2011 17:56
Show Gist options
  • Save craigspaeth/1087868 to your computer and use it in GitHub Desktop.
Save craigspaeth/1087868 to your computer and use it in GitHub Desktop.
Coffescript override Backbone.sync to be Ruby on Rails friendly
# 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