Skip to content

Instantly share code, notes, and snippets.

@bradhe
Created June 7, 2012 18:01
Show Gist options
  • Save bradhe/2890461 to your computer and use it in GitHub Desktop.
Save bradhe/2890461 to your computer and use it in GitHub Desktop.
Backbone patch for Rails. Makes it play nice with Rails' verb expectations, adds ability to ignore attributes, adds ability to generate attributes on a callback, and adds ability to specify a format that we want to send back/forth.
(function() {
var methodMap = {
'create': 'POST',
'update': 'PUT',
'delete': 'DELETE',
'read' : 'GET'
};
var getUrl = function(object) {
if (!(object && object.url)) return null;
return _.isFunction(object.url) ? object.url() : object.url;
};
var urlError = function() {
throw new Error("A 'url' property or function must be specified");
};
Backbone.sync = function(method, model, options) {
var type = methodMap[method];
// Default JSON-request options.
var params = _.extend({
type: type,
dataType: 'json',
beforeSend: function( xhr ) {
var token = $('meta[name="csrf-token"]').attr('content');
if (token) xhr.setRequestHeader('X-CSRF-Token', token);
model.trigger('sync:start');
}
}, options);
if (!params.url) {
params.url = getUrl(model) || urlError();
}
// Add the request format in here if need be.
if(model.format) {
// Make sure the URL lines up here.
var format = model.format.indexOf('.') == 0 ? model.format : "."+model.format;
if(params.url.substr(-1 * format.length) != format) {
params.url = params.url + format;
}
}
// Ensure that we have the appropriate request data.
if (!params.data && model && (method == 'create' || method == 'update')) {
params.contentType = 'application/json';
var data = {}
if(model.paramRoot) {
data[model.paramRoot] = model.toJSON();
} else {
data = model.toJSON();
}
if(model.ignoreParams) {
for(var i in model.ignoreParams) {
var param = model.ignoreParams[i];
if(model.paramRoot) {
delete data[model.paramRoot][param];
}
else {
delete data[param];
}
}
}
params.data = JSON.stringify(data)
}
// Don't process data on a non-GET request.
if (params.type !== 'GET') {
params.processData = false;
}
// Trigger the sync end event
var complete = options.complete;
options.complete = function(jqXHR, textStatus) {
model.trigger('sync:end');
if (complete) complete(jqXHR, textStatus);
};
// Make the request.
return $.ajax(params);
}
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment