firstChild
firstChild

Reputation: 356

Is it possible to check condition before all http calls in Angular?

Is it possible to check if: $rootScope.variable is TRUE

Before all $http calls are called, or I should check for every individual call? Some of my calls are call through angular factory, and some are not.

I thought there is maybe some way like httpInterceptor which would check before some call is ran.

Any help would appreciated.

Upvotes: 1

Views: 1935

Answers (2)

Callum Linington
Callum Linington

Reputation: 14417

To further @boindiil's answer. I tend to have it like this:

angular.module('someModule', [])
    .factory('httpInterceptorService', ['$q', httpInterceptorService])
    .config(['$httpProvider', interceptorConfig]);

function httpInterceptorService($q) {
    var service = {
        request: request,
        responseError: responseError
    };

    return service;

    function request(config) {
        // do some logic
        return config;
    }

    function responseError(rejection) {
        if (rejection.status === 401) {
             // they were unauthorised.
        }

        return $q.reject(rejection);
    }
}

function interceptorConfig ($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptorService');
}

Here it is more separated out. And you can see how you can easily push more interceptors into the pipeline. Obviously you can inject what ever you like into the httpInterceptorService like the $rootScope.

Just beware not create any circular dependencies.

I like what @pankajparkar commented about, maintaining a proper call stack.

You can do this instead of using interceptors (as they are for every request).

angular.module('someModule', [])
    .factory('mainService', ['$http', '$rootScope', '$q', mainService])
    .controller('MainCtrl', ['mainService', mainCtrl]);

function mainService($http, $rootScope, $q) {
    var service = {
        getThings: getThings
    };

    var serviceBase = '/Api/Things';

    return service;

    function getThings() {
        var deferred = $q.defer();

        $http.get(serviceBase).then(function (data) {
            if (data.data.someVariable == $rootScope.someVariable) {
                deferred.resolve(data.data);
            } else {
                deferred.reject(data);
            }
        }).catch(function (message) {
           deferred.reject(message);
        });

        return deferred.promise;
    }
}

function mainCtrl(mainService) {
    var vm = this;

    vm.httpData = {};

    mainService.getThings().then(function (data) {
        vm.httpData = data;
    }, function (message) {
        // do something with the error.
    });
}

Upvotes: 1

boindiil
boindiil

Reputation: 5865

You can create an interceptor for this issue like this one:

angular.module('myModule', []).config(function($httpProvider) { 
    $httpProvider.interceptors.push(function($rootScope) {
        return {
            'request': function(config) {
                if($rootScope.yourVariable) {
                    // cancel this request
                }
            }
        }
    });
})

This interceptor processes every request. You can find an implementation for cancelling requests here

Upvotes: 3

Related Questions