ui-router onEnter替代方案

时间:2015-06-12 16:52:00

标签: javascript angularjs angular-ui-router

我正在尝试根据用户最近访问子路由动态加载路由。最近的访问被保存并使用工厂检索。目前我正在使用onEnter,但这导致父路由加载,然后第二次加载恰好加载子路由。有没有办法防止第二次加载,如果有所需的信息,就直接进入子路径?

(function() {
    'use strict';

    angular
        .module('curation')
        .config(routesConfig);

    routesConfig.$inject = ['$stateProvider'];
    /**
     * Route Configuration to establish url patterns for the module
     * @param {Function} $stateProvider
     */
    function routesConfig($stateProvider) {
        // Curation state routing
        $stateProvider.
        state('site.curation', {
            url: '/curation',
            templateUrl: 'modules/curation/views/curation.client.view.html',
            resolve: {
                publicFronts: function($stateParams, frontsService) {
                    return frontsService.getPublicFronts($stateParams.siteCode);
                },
                authoringTypes: function(assetMetadata) {
                    return assetMetadata.authoringTypes().then(function(value) {
                        return value.data.authoringTypes;
                    });
                }
            },
            onEnter: function($state, $timeout, userSitePreferences, publicFronts) {
                $timeout(function() {
                    var recentFront = _.find(publicFronts, {
                        Id: userSitePreferences.curation.recentFrontIds[0]
                    });
                    if (recentFront) {
                        $state.go('site.curation.selectedFront.selectedLayout', {
                            frontId: recentFront.Id,
                            layoutId: recentFront.LayoutId
                        });
                    }
                });
            }
        });
    }
})();

1 个答案:

答案 0 :(得分:0)

尝试"深度状态重定向" " ui-router-extras"。

http://christopherthielen.github.io/ui-router-extras/#/dsr

尝试这样的事情......

    $stateProvider.
    state('site.curation', {
        url: '/curation',
        templateUrl: 'modules/curation/views/curation.client.view.html',
        resolve: {
            publicFronts: function($stateParams, frontsService) {
                return frontsService.getPublicFronts($stateParams.siteCode);
            },
            authoringTypes: function(assetMetadata) {
                return assetMetadata.authoringTypes().then(function(value) {
                    return value.data.authoringTypes;
                });
            }
        },
        deepStateRedirect: {
            default: {
                state: 'site.curation.selectedFront.selectedLayout'
                params: {
                    frontId: 'defaultId',
                    layoutId: 'defaultLayout'
                }
            },
            params: true,
            // I've never tried to generate dynamic params, so I'm not sure
            // that this accepts DI variables, but it's worth a try.
            fn: function ($dsr$, userSitePreferences, publicFronts) {
                var recentFront = _.find(publicFronts, {
                    Id: userSitePreferences.curation.recentFrontIds[0]
                });
                if (recentFront) {
                    return {
                        state: $dsr$.redirect.state,
                        params: {
                            frontId: recentFront.Id,
                            layoutId: recentFront.LayoutId
                        }
                    };
                } else {
                    return false;
                }
            });
        }
    });