Skip to content

Instantly share code, notes, and snippets.

@DavidQL
Last active December 27, 2015 01:19
Show Gist options
  • Save DavidQL/7244134 to your computer and use it in GitHub Desktop.
Save DavidQL/7244134 to your computer and use it in GitHub Desktop.
Backbone.ajax = function(params) {
var request_type = params.type,
url = params.url,
deferred = $.Deferred();
if (request_type === "GET") {
appAPI.request.get({
url: url,
onSuccess:function(response, additional_info) {
params.success(JSON.parse(response));
deferred.resolveWith(this, [JSON.parse(response), additional_info]);
},
onFailure: function(httpCode) {
params.error(httpCode);
deferred.rejectWith(this, [httpCode]);
}
});
} else if (request_type === "POST") {
appAPI.request.post({
url: url,
postData: JSON.parse(params.data),
onSuccess: function(response) {
params.success(JSON.parse(response));
deferred.resolveWith(this, [JSON.parse(response)]);
},
onFailure: function(httpCode) {
params.error(httpCode);
deferred.rejectWith(this, [httpCode]);
}
});
}
return deferred;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment