stop angular-ui-router navigation until promise is resolved

前端 未结 10 1951
情歌与酒
情歌与酒 2020-12-01 02:44

I want to prevent some flickering that happens when rails devise timeout occurs, but angular doesn\'t know until the next authorization error from a resource.

What h

10条回答
  •  自闭症患者
    2020-12-01 03:26

    I really like the suggested solution by TheRyBerg, since you can do all in one place and without too much weird tricks. I have found that there is a way to improve it even further, so that you don't need the stateChangeBypass in the rootscope. The main idea is that you want to have something initialized in your code before your application can "run". Then if you just remember if it's initialized or not you can do it this way:

    rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState) {
    
        if (dataService.isInitialized()) {
            proceedAsUsual(); // Do the required checks and redirects here based on the data that you can expect ready from the dataService
        } 
        else {
    
            event.preventDefault();
    
            dataService.intialize().success(function () {
                    $state.go(toState, toParams);
            });
        }
    });
    

    Then you can just remember that your data is already initialized in the service the way you like, e.g.:

    function dataService() {
    
        var initialized = false;
    
        return {
            initialize: initialize,
            isInitialized: isInitialized
        }
    
        function intialize() {
    
            return $http.get(...)
                        .success(function(response) {
                                initialized=true;
                        });
    
        }
    
        function isInitialized() {
            return initialized;
        }
    };
    

提交回复
热议问题