Created
October 29, 2014 15:57
-
-
Save jimschubert/02d45b63c0d79c0afd32 to your computer and use it in GitHub Desktop.
Tweaking $http response without a response interceptor (AngularJS)
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
function makeRequest(){ | |
// var config = {...} | |
var query = $http(config); | |
query.then(function onSuccessIntermediate(response) { | |
// When this HTTP response comes back, we will use this function to tweak any data | |
var deferred = $q.defer(); | |
var promise = deferred.promise; | |
// $http has a success and error helper function, so we need to make this promise look the same. | |
promise.success = function(fn) { | |
promise.then(function(response) { | |
fn(response.data, response.status, response.headers, config); | |
}); | |
return promise; | |
}; | |
promise.error = function(fn) { | |
promise.then(null, function(response) { | |
fn(response.data, response.status, response.headers, config); | |
}); | |
return promise; | |
}; | |
if (angular.isObject(response) && angular.isObject(response.data)) { | |
// Modify response.data, then resolve | |
// response.data.touched = 1; | |
deferred.resolve(response); | |
} | |
return promise; | |
}); | |
return query; | |
} | |
/* | |
This can be used just like $http: | |
makeRequest() | |
.success(function(data){}) | |
.error(function(data){}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment