Skip to content

Instantly share code, notes, and snippets.

@prayagverma
Forked from jintoppy/GlobalAjaxInterceptor
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save prayagverma/b4ac6539ae5e8993d24f to your computer and use it in GitHub Desktop.

Select an option

Save prayagverma/b4ac6539ae5e8993d24f to your computer and use it in GitHub Desktop.

Revisions

  1. prayagverma renamed this gist Jul 17, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @jintoppy jintoppy revised this gist Aug 20, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions GlobalAjaxInterceptor
    Original file line number Diff line number Diff line change
    @@ -55,8 +55,8 @@ angular.module('globalmodule')
    config.timeout = deferred.promise;

    addHttpRequest({url: config.url, promiseObj: deferred});
    // Return the config or wrap it in a promise if blank.
    return config || $q.when(config);

    return config;
    },

    // On request failure
  3. @jintoppy jintoppy revised this gist Aug 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GlobalAjaxInterceptor
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ angular.module('globalmodule')
    function abortAllAjaxRequests(ajaxRequests){
    angular.forEach(ajaxRequests, function(xhr, url){
    xhr && xhr.abort();
    })
    });
    }

    function abortAllOldRequests(){
  4. @jintoppy jintoppy created this gist Aug 18, 2014.
    83 changes: 83 additions & 0 deletions GlobalAjaxInterceptor
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    angular.module('globalmodule')
    .config(['$provide', '$httpProvider', function($provide, $httpProvider){

    $provide.factory('GlobalAjaxInterceptor', ['$q', '$rootScope', function($q, $rootScope){

    var currentRequests={http: {}, ajax: {}};

    function addHttpRequest(conf){
    currentRequests.http[conf.url] = conf.promiseObj;
    }

    function addAjaxRequest(conf){
    currentRequests.ajax[conf.url] = conf.promiseObj;
    }


    function abortAllHttpRequests(httpRequests){
    angular.forEach(httpRequests, function(promise, url){
    promise && promise.resolve();
    });

    }

    function abortAllAjaxRequests(ajaxRequests){
    angular.forEach(ajaxRequests, function(xhr, url){
    xhr && xhr.abort();
    })
    }

    function abortAllOldRequests(){
    var oldRequests = angular.copy(currentRequests);

    currentRequests = {
    http: {},
    ajax: {}
    };

    abortAllHttpRequests(oldRequests.http);
    abortAllAjaxRequests(oldRequests.ajax);

    }

    $( document ).ajaxSend(function(event, xhr, options) {
    addAjaxRequest({url: options.url, promiseObj: xhr});
    });

    $rootScope.$on('$stateChangeSuccess', function () {
    abortAllOldRequests();
    });

    return {
    // On request success
    request: function (config) {
    var deferred = $q.defer();
    config.timeout = deferred.promise;

    addHttpRequest({url: config.url, promiseObj: deferred});
    // Return the config or wrap it in a promise if blank.
    return config || $q.when(config);
    },

    // On request failure
    requestError: function (rejection) {
    // Return the promise rejection.
    return $q.reject(rejection);
    },

    // On response success
    response: function (response) {
    // Return the response or promise.
    return response || $q.when(response);
    },

    // On response failture
    responseError: function (rejection) {
    // Return the promise rejection.
    return $q.reject(rejection);
    }
    };
    }]);

    $httpProvider.interceptors.push('GlobalAjaxInterceptor');
    }]);