How can I perform an AJAX request before my angular app's routing kicks in?

Chad Johnson

I need to perform an AJAX request against my API for authentication information before routing kicks in and routing data requests resolve (I'm using a server-to-server OpenID auth solution).

I initialize my app like this currently:

angular.module('mynamespace.admin', [
    'ngRoute',
    'ui.bootstrap',
    'mynamespace.directives',
    'mynamespace.filters',
    'mynamespace.factories',
    'mynamespace.resources',
    'mynamespace.admin.templates',
    'mynamespace.admin.forms'
]);

angular.module('mynamespace.admin').run(['$rootScope', '$http', 'base64', function($rootScope, $http, base64) {
    if (!window.location.origin) {
      window.location.origin = window.location.protocol + '//' + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
    }

    // I NEED THIS TO FINISH BEFORE ROUTES RUN AND PERFORM
    // HTTP REQUESTS TO RESOLVE DATA
    $http.get(window.location.origin + '/account').success(function(accountData) {
        $rootScope.accountData = accountData;

        // Set up auth headers to be sent with every HTTP request.
        $http.defaults.headers.common = { 'Access-Control-Request-Headers': 'accept, origin, authorization' };
        $http.defaults.headers.common['Authorization'] = 'Basic ' + base64.encode(accountData.apiKey + ':' + accountData.apiPassword);
    });
}]);

angular.module('mynamespace.admin').config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', {  // THIS IS RUNNING BEFORE THE $http REQUEST in .run() finishes
            redirectTo: '/forms'
        });
}]);

Routing kicks in at the .config() callback at the end. Unfortunately the /forms route contains routes with resolve items which end up making $http requests before I set up the common $http headers inside the .run() callback. Basically I have a race condition.

So, my question is: How can I execute and complete an AJAX request and ensure it returns BEFORE my app's routing kicks in and routes start making AJAX requests?

If this question is somehow unclear, I ask that you please request clarification, and I will clarify.

Saidh

Use routeChangeStart event of angular.

var checkAuthentication = function(event, routeChange) {
    routeChange.$$route.resolve = {
        app: function($q) {
            var deferred = $q.defer();
            //Authentication({deferred.resolve();})
            return deferred.promise;
        }
    }
}

$rootScope.$on('$routeChangeStart', checkAuthenticationFn);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I turn all my 'perform_later's into 'perform_now's locally?

From Dev

How can I do something before uninstalling my android app?

From Dev

How can I wait until my AJAX request is complete?

From Dev

How can I wait until my AJAX request is complete?

From Java

How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

From Dev

How can I make first Ajax Get request be finish before I call next one?

From Dev

How can I call my validate function before sending my ajax when a button is clicked?

From Dev

How can I get my app to get the URL of a link clicked in WebView, perform operations on it, and then load it?

From Dev

How can I check my Android app's network consumption?

From Dev

How can I reduce my app's "Idle Wake Ups"?

From Dev

How can resolve some stuff/ajax call before to display my view in Angular 2?

From Dev

In Angular, how can I manipulate my result objects before my http get returns them?

From Dev

How can I perform a check before sbt assembly

From Dev

How can I keep my app's notification displayed when my app is updated on Google Play store?

From Dev

How i can perform request to tarantool like in mysql?

From Dev

How i can perform request to tarantool like in mysql?

From Dev

how can I cancel an Ajax request?

From Dev

How can I posts parameter to an ajax request?

From Dev

How to assign Promise to JSON before await kicks

From Dev

My ISP responded to my request with another user's content, is that permissible? And if not, how can I prove it?

From Dev

How can I stop having to type "$scope" before every variable and function in my Angular code?

From Dev

How can I manipulate an object derived from a JSON response before my angular view is updated?

From Dev

How can I schedule hourly request to a server and store data on my own server to access from iOS app?

From Dev

How can I request an unknown number of fields from my python code in App Engine?

From Dev

How can I perform my part of code using map and match?

From Dev

How can I perform a linear regression on my group variances in R?

From Dev

How can I get my SSDs write speeds to perform normally?

From Dev

How can I set up my model to perform this transaction?

From Dev

I can't retrieve my data from request AJAX in Laravel

Related Related

  1. 1

    How can I turn all my 'perform_later's into 'perform_now's locally?

  2. 2

    How can I do something before uninstalling my android app?

  3. 3

    How can I wait until my AJAX request is complete?

  4. 4

    How can I wait until my AJAX request is complete?

  5. 5

    How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

  6. 6

    How can I make first Ajax Get request be finish before I call next one?

  7. 7

    How can I call my validate function before sending my ajax when a button is clicked?

  8. 8

    How can I get my app to get the URL of a link clicked in WebView, perform operations on it, and then load it?

  9. 9

    How can I check my Android app's network consumption?

  10. 10

    How can I reduce my app's "Idle Wake Ups"?

  11. 11

    How can resolve some stuff/ajax call before to display my view in Angular 2?

  12. 12

    In Angular, how can I manipulate my result objects before my http get returns them?

  13. 13

    How can I perform a check before sbt assembly

  14. 14

    How can I keep my app's notification displayed when my app is updated on Google Play store?

  15. 15

    How i can perform request to tarantool like in mysql?

  16. 16

    How i can perform request to tarantool like in mysql?

  17. 17

    how can I cancel an Ajax request?

  18. 18

    How can I posts parameter to an ajax request?

  19. 19

    How to assign Promise to JSON before await kicks

  20. 20

    My ISP responded to my request with another user's content, is that permissible? And if not, how can I prove it?

  21. 21

    How can I stop having to type "$scope" before every variable and function in my Angular code?

  22. 22

    How can I manipulate an object derived from a JSON response before my angular view is updated?

  23. 23

    How can I schedule hourly request to a server and store data on my own server to access from iOS app?

  24. 24

    How can I request an unknown number of fields from my python code in App Engine?

  25. 25

    How can I perform my part of code using map and match?

  26. 26

    How can I perform a linear regression on my group variances in R?

  27. 27

    How can I get my SSDs write speeds to perform normally?

  28. 28

    How can I set up my model to perform this transaction?

  29. 29

    I can't retrieve my data from request AJAX in Laravel

HotTag

Archive