Skip to content

Instantly share code, notes, and snippets.

@ch3stnut
ch3stnut / sync.js.coffee
Last active August 29, 2015 13:58
For BackboneRails series. Code that adds the CSRF token to CRUD requests automatically, merged with the original sync config file by BackboneRails.
do (Backbone) ->
Backbone._sync = Backbone.sync
Backbone.sync = (method, entity, options={}) ->
_.defaults options,
beforeSend: _.bind( methods.beforeSend, entity )
complete: _.bind( methods.complete, entity )
if method is 'create' or method is 'update' or method is 'put' or method is 'delete'
options.headers ?= {}
Backbone._sync = Backbone.sync
Backbone.sync = (method, model, options) ->
if method is 'create' or method is 'update' or method is 'put' or method is 'delete'
options.headers ?= {}
options.headers["X-CSRF-Token"] = $("meta[name='csrf-token']").attr 'content'
return Backbone._sync(method, model, options);