Last active
August 29, 2015 13:58
-
-
Save ch3stnut/10364526 to your computer and use it in GitHub Desktop.
For BackboneRails series. Code that adds the CSRF token to CRUD requests automatically, merged with the original sync config file by BackboneRails.
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
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 ?= {} | |
options.headers["X-CSRF-Token"] = $("meta[name='csrf-token']").attr 'content' | |
sync = Backbone._sync(method, entity, options) | |
if !entity._fetch and method is "read" | |
entity._fetch = sync | |
sync | |
methods = | |
beforeSend: -> | |
@trigger "sync:start", @ | |
complete: -> | |
@trigger "sync:stop", @ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment