forked from codebrew/backbone-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from jarrett/master
Fix codebrew#188
- Loading branch information
Showing
1 changed file
with
53 additions
and
17 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,68 @@ | ||
(function($) { | ||
Backbone._sync = Backbone.sync; | ||
(function() { | ||
var origSync = Backbone.sync; | ||
|
||
// Define a patched version of Backbone.Model.toJSON that takes into account paramRoot. | ||
// Note that toJSON is not called when you call save with {patch: true}. So we handle | ||
// that case elsewhere. | ||
function wrapToJSON(origToJSON) { | ||
return function(options) { | ||
// This is the attributes hash formatted in the Backbone standard way. | ||
var unwrapped = origToJSON.apply(this, [options]); | ||
if (this.paramRoot) { | ||
// For this model, we want to wrap the attributes hash in a parameter name, as | ||
// expected by Rails. | ||
var json = {}; | ||
json[this.paramRoot] = unwrapped; | ||
return json; | ||
} else { | ||
// This model doesn't define paramRoot, so we stick with the default | ||
// Backbone behavior. | ||
return unwrapped; | ||
} | ||
} | ||
} | ||
|
||
|
||
// Patch Backbone.sync so that it 1) uses the csrf-token, and 2) uses our patched | ||
// version of toJSON. | ||
Backbone.sync = function(method, model, options) { | ||
if (!options.noCSRF) { | ||
var beforeSend = options.beforeSend; | ||
|
||
// Set X-CSRF-Token HTTP header | ||
options.beforeSend = function(xhr) { | ||
var token = $('meta[name="csrf-token"]').attr('content'); | ||
var token = jQuery('meta[name="csrf-token"]').attr('content'); | ||
if (token) xhr.setRequestHeader('X-CSRF-Token', token); | ||
if (beforeSend) return beforeSend.apply(this, arguments); | ||
}; | ||
} | ||
|
||
// Serialize data, optionally using paramRoot | ||
if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) { | ||
options.contentType = 'application/json'; | ||
data = JSON.stringify(options.attrs || model.toJSON(options)); | ||
|
||
if (options.attrs) { | ||
// sync wasn't called with options.attrs. So Model.save was called with | ||
// {patch: true}. | ||
if (model.paramRoot) { | ||
data = {}; | ||
data[model.paramRoot] = model.toJSON(options); | ||
} else { | ||
data = model.toJSON(); | ||
// Wrap options.attrs in paramRoot and pass control to the original Backbone.sync. | ||
var wrapped = {}; | ||
wrapped[model.paramRoot] = options.attrs; | ||
options.attrs = wrapped; | ||
} | ||
return origSync(method, model, options); | ||
} else { | ||
// sync wasn't called with options.attrs. So Model.save wasn't called with | ||
// {patch: true}. | ||
// | ||
// Temporarily patch Backbone.Model.toJSON so it takes into account paramRoot. | ||
// Restore the original version of toJSON after we're done. | ||
var ret; | ||
try { | ||
var origToJSON = model.toJSON; | ||
model.toJSON = wrapToJSON(origToJSON); | ||
ret = origSync(method, model, options); | ||
} finally { | ||
model.toJSON = origToJSON; | ||
} | ||
options.data = JSON.stringify(data); | ||
} | ||
|
||
return Backbone._sync(method, model, options); | ||
return ret; | ||
}; | ||
|
||
})(jQuery); | ||
})(); |