Last active
December 17, 2015 13:39
-
-
Save tarmann/5618587 to your computer and use it in GitHub Desktop.
JavaScript: Backbone Sync for legacy web server
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
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