Browse Source

#2092 Add onResponse callback to allow for response transformations

pull/2169/head
jlukic 9 years ago
parent
commit
e441bebedd
1 changed files with 17 additions and 7 deletions
  1. 24
      src/definitions/behaviors/api.js

24
src/definitions/behaviors/api.js

@ -137,7 +137,7 @@ $.api = $.fn.api = function(parameters) {
} }
// call beforesend and get any settings changes // call beforesend and get any settings changes
requestSettings = module.get.settings();
requestSettings = module.get.settings();
// check if before send cancelled request // check if before send cancelled request
if(requestSettings === false) { if(requestSettings === false) {
@ -366,11 +366,18 @@ $.api = $.fn.api = function(parameters) {
settings.onComplete.call(context, response, $module); settings.onComplete.call(context, response, $module);
}, },
done: function(response) { done: function(response) {
var
translatedResponse = ( $.isFunction(settings.onResponse) )
? settings.onResponse.call(context, $.extend(true, {}, response))
: false
;
module.debug('API Response Received', response); module.debug('API Response Received', response);
if( $.isFunction(settings.filter) ) {
response = settings.filter(response);
module.debug('Response filter applied', settings.filter, response);
if(translatedResponse) {
module.debug('Modified API response in onResponse callback', settings.onResponse, translatedResponse, response);
response = translatedResponse;
} }
if(settings.dataType == 'json') { if(settings.dataType == 'json') {
if( $.isFunction(settings.successTest) ) { if( $.isFunction(settings.successTest) ) {
module.debug('Checking JSON returned success', settings.successTest, response); module.debug('Checking JSON returned success', settings.successTest, response);
@ -438,6 +445,7 @@ $.api = $.fn.api = function(parameters) {
}, },
create: { create: {
// api promise
request: function() { request: function() {
return $.Deferred() return $.Deferred()
.always(module.event.request.complete) .always(module.event.request.complete)
@ -445,6 +453,7 @@ $.api = $.fn.api = function(parameters) {
.fail(module.event.request.error) .fail(module.event.request.error)
; ;
}, },
// xhr promise
xhr: function() { xhr: function() {
return $.ajax(ajaxSettings) return $.ajax(ajaxSettings)
.always(module.event.xhr.always) .always(module.event.xhr.always)
@ -818,18 +827,19 @@ $.api.settings = {
data : {}, data : {},
dataType : 'json', dataType : 'json',
// callbacks
// callbacks before request
beforeSend : function(settings) { return settings; }, beforeSend : function(settings) { return settings; },
beforeXHR : function(xhr) {}, beforeXHR : function(xhr) {},
onRequest : function(promise, xhr) {}, onRequest : function(promise, xhr) {},
// after request
onResponse : function(response) { response.id = 2; return response; },
onSuccess : function(response, $module) {}, onSuccess : function(response, $module) {},
onComplete : function(response, $module) {}, onComplete : function(response, $module) {},
onFailure : function(errorMessage, $module) {}, onFailure : function(errorMessage, $module) {},
onError : function(errorMessage, $module) {}, onError : function(errorMessage, $module) {},
onAbort : function(errorMessage, $module) {}, onAbort : function(errorMessage, $module) {},
filter : false,
successTest : false, successTest : false,
// errors // errors

Loading…
Cancel
Save