0

我已经完成了一个设计,其中包含uaContext一些有用的信息,例如productIdoperatorId. 我需要这些信息来显示每个页面的内容。

myApp.service('uaContext', function($cookieStore){
    this.init = function() {
        this.isLogged = new Field();
        this.userName = new CookieField('userName', $cookieStore);

        this.appName = new Field();
        this.subAppName = new Field();

        this.operator = new Field();
        this.operatorList = new FilterField();

        this.product = new Field();
        this.productList = new FilterField();
    };

    this.init();
});

为了确保我掌握了这些信息,我抓住了全球$routeChangeStart事件:

myApp.run( function($rootScope, $routeParams, uaViewProvider, uaAuthService, uaContext){
    $rootScope.$on('$routeChangeStart', function (event, next) {
        if (uaContext.isLogged.get() == false && next.loginRequired != false){
            console.log('View requires to be logged in');
            if (uaAuthService.sessionCookieExists() == true){
                console.log('Recover session from Cookie');

                uaAuthService.loadSession().then(function() {
                    uaContext.operator.set(uaContext.operatorList.filter('id', +$routeParams.operatorId));
                    uaContext.productList.set(uaContext.operator.get().products);
                    uaContext.product.set(uaContext.productList.filter('id', +$routeParams.productId));
                });
            } else{
                console.log('User not logged in. Redirect to login.');
                uaViewProvider.redirectToView('login');
            }
        }

    });
});

loadSession在这种情况下,当我通过 cookie 恢复会话时,我请求使用promise重新加载会话。然后我根据 url 参数定义productIdand 。operatorId

问题是,在请求更多信息之前,我需要获取这些参数:

var myApp2 = angular.module('ua.myApp2', []);

myApp2.controller('ua.myApp2Controller',
    function ($scope, $rootScope, $routeParams, uaContext, uaApiInterface) {
        $scope.$on('$routeChangeSuccess', function () {
            uaContext.appName.set('Settings');
            uaContext.subAppName.set('Sims');

        });

        console.log('On route change');

        $scope.$watch(function(){
            watchValue = uaContext.operator.get().id + '-' + uaContext.product.get().id
            return watchValue;
        }, function(){
            uaApiInterface.getSimList(uaContext.operator.get().id,uaContext.product.get().id).then(
                function(objects) {
                    console.log('Then');
                    $scope.simList = objects.data.objects;
                    console.log($scope.simList);
                    console.log(objects.data.objects);

                }
            )
        },true);
    }
);

我发现等待承诺结束的唯一解决方案是在andloadSession上放一个手表。operatorIdproductId

问题是我必须为我的所有观点定义这款手表。这真的不干,所以我的问题是如何优化它?

4

1 回答 1

0

我发现的最佳解决方案是放置以下代码:

uaAuthService.loadSession().then(function() {
    uaContext.operator.set(uaContext.operatorList.filter('id', +$routeParams.operatorId));
    uaContext.productList.set(uaContext.operator.get().products);
    uaContext.product.set(uaContext.productList.filter('id', +$routeParams.productId));
})

在上下文服务中。

那么在每个视图中,入口点是:

uaContext.getSession().then(...

如果您有任何意见或建议。

于 2013-10-22T09:37:50.757 回答