angularjs multiple resolve with loadsequence and auth permission

时间:2016-10-15 17:01:27

标签: javascript angularjs angular-ui-router firebase-authentication

Referring to this question here, I am having the same problem.
How can we set up a multiple resolve, one with currentAuth and another with loadsequence which loads controllers and scripts?


Here is my state config :

.state('app.example', {
        url: "/example",
        templateUrl: "assets/views/example.html",
        resolve:{
            loadSequence: loadSequence('jquery-sparkline', 'exampleCtrl')
        },
        title: 'example',
        ncyBreadcrumb: {
            label: 'example'
        }
    })


and here is my loadsequece function:

function loadSequence() {
    var _args = arguments;
    return {
        deps: ['$ocLazyLoad', '$q',
        function ($ocLL, $q) {
            var promise = $q.when(1);
            for (var i = 0, len = _args.length; i < len; i++) {
                promise = promiseThen(_args[i]);
            }
            return promise;

            function promiseThen(_arg) {
                if (typeof _arg == 'function')
                    return promise.then(_arg);
                else
                    return promise.then(function () {
                        var nowLoad = requiredData(_arg);
                        if (!nowLoad)
                            return $.error('Route resolve: Bad resource name [' + _arg + ']');
                        return $ocLL.load(nowLoad);
                    });
            }

            function requiredData(name) {
                if (jsRequires.modules)
                    for (var m in jsRequires.modules)
                        if (jsRequires.modules[m].name && jsRequires.modules[m].name === name)
                            return jsRequires.modules[m];
                return jsRequires.scripts && jsRequires.scripts[name];
            }
        }]
    };
}


and here is my currentAuth factory:

currentAuth: ['Auth', function(Auth) {
                return Auth.$requireSignIn()
            }]

1 个答案:

答案 0 :(得分:1)

documentation of ui-router

中所述
  

resolve属性是一个地图对象。 map对象包含键/值对:

     

key - {string}:要注入控制器的依赖项的名称。

     

factory - {string | function}:   如果是string,那么它是服务的别名。   否则,如果是函数,则将其注入,并将返回值视为依赖项。如果结果是一个promise,则在实例化控制器并将其值注入控制器之前解析它。

因此您可以在州决议中配置州添加功能:

.state('app.example', {
    url: "/example",
    templateUrl: "assets/views/example.html",
     resolve: { 
scripts: loadSequence('jquery-sparkline', 'exampleCtrl').deps,
currentAuth: function(Auth){ return Auth.$requireSignIn();}
},
    title: 'example',
    ncyBreadcrumb: {
        label: 'example'
    }
})