Skip to content

Instantly share code, notes, and snippets.

@tarmann
Last active December 17, 2015 13:39
Show Gist options
  • Save tarmann/5618587 to your computer and use it in GitHub Desktop.
Save tarmann/5618587 to your computer and use it in GitHub Desktop.
JavaScript: Backbone Sync for legacy web server
Backbone.sync = function (method, model, options) {
var params = _.extend({
type: 'POST',
dataType: 'json',
url: model.url,
traditional: true,
contentType: 'application/x-www-form-urlencoded;charset=UTF-8'
}, options);
if (method == 'read') {
params.type = 'GET';
params.data = model.id
}
if (!params.data && model && (method == 'create' || method == 'update' || method == 'delete')) {
var urlParams = _( model.toJSON() ).serialize();
$('.request').html( urlParams );
}
if (params.type !== 'GET') {
params.processData = false;
}
return $.ajax(params);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment