Skip to content

Instantly share code, notes, and snippets.

@hokaccha
Created December 18, 2012 00:17
Show Gist options
  • Save hokaccha/4323692 to your computer and use it in GitHub Desktop.
Save hokaccha/4323692 to your computer and use it in GitHub Desktop.
Backbone.sync = function(method, model, options) {
var type = methodMap[method];
// Default options, unless specified.
options || (options = {});
// Default JSON-request options.
var params = {type: type, dataType: 'json'};
// Ensure that we have a URL.
if (!options.url) {
params.url = getValue(model, 'url') || urlError();
}
// Ensure that we have the appropriate request data.
if (!options.data && model && (method == 'create' || method == 'update')) {
params.data = model.toJSON();
}
if (type === 'PUT' || type === 'DELETE') {
params.type = 'POST';
params.beforeSend = function(xhr) {
xhr.setRequestHeader('X-HTTP-Method-Override', type);
};
}
// Make the request, allowing the user to override any Ajax options.
return $.ajax(_.extend(params, options));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment