Created
October 13, 2017 13:50
-
-
Save joeRinehart/93a7596e7acd85bda5dbf99bdd4bde24 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
.config(['$httpProvider', function($httpProvider) { | |
// Create a new definition for the httpProvider's factory function that allows us to intercept the | |
// $http service it creates | |
var originalFactoryFunction = $httpProvider.$get[ $httpProvider.$get.length - 1 ] | |
var newFactoryFunction = function($browser, $httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector, $sce) { | |
var $http = originalFactoryFunction( $browser, $httpBackend, $$cookieReader, $cacheFactory, $rootScope, $q, $injector, $sce ) | |
// Decorate the definition of .get() | |
var originalGetDefinition = $http.get | |
$http.get = function(url, config) { | |
// Invoke the original get and stash the it returns | |
var promise = originalGetDefinition(url, config) | |
// Create a .success() method that uses itself to add a then() that delegates... | |
promise.success = function(handlerFunction) { | |
promise.then( | |
function(response) { | |
handlerFunction( response.data ) | |
} | |
) | |
return promise | |
} | |
// Create an .error() method that uses itself to add a then() that delegates... | |
promise.error = function(handlerFunction) { | |
// Handle it in the 'then' to match old angular... | |
promise.then( | |
null, | |
function(response) { | |
handlerFunction( response.data ) | |
} | |
) | |
return promise | |
} | |
return promise | |
} | |
return $http | |
} | |
// Redefine $get for $httpProvider | |
$httpProvider.$get = ['$browser', '$httpBackend', '$$cookieReader', '$cacheFactory', '$rootScope', '$q', '$injector', '$sce', newFactoryFunction] | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment