-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy pathbackbone_rails_sync.js
More file actions
68 lines (62 loc) · 2.28 KB
/
backbone_rails_sync.js
File metadata and controls
68 lines (62 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(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 = jQuery('meta[name="csrf-token"]').attr('content');
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
if (beforeSend) return beforeSend.apply(this, arguments);
};
}
if (options.attrs) {
// sync wasn't called with options.attrs. So Model.save was called with
// {patch: true}.
if (model.paramRoot) {
// 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;
}
}
return ret;
};
})();