简体   繁体   English

角1.2的热毛巾,玩菜单

[英]hot towel angular 1.2, playing with the menu

               Hi all,

Environment : vs 2013 express for web, hot towel angular 1.2, Breeze.WebApi2, sql server 2012 express. 环境:vs 2013 Express for Web,热毛巾角1.2,Breeze.WebApi2,SQL Server 2012 Express。

I would like to be able to add or remove items in the menu of my web app, would it be possible ? 我希望能够在Web应用程序的菜单中添加或删除项目,这可能吗?

I've seen the config.route.js where the menu is built and returned by getRoutes() : 我看过config.route.js,其中菜单是由getRoutes()构建并返回的:

function getRoutes() {

    var rts = [
        {
            url: '/',
            config: {
                templateUrl: 'app/dashboard/dashboard.html',
                title: 'dashboard',
                settings: {
                    nav: 2,
                    content: '<i class="fa fa-dashboard"></i> Dashboard'
                }
            }
        },
        {
            url: '/admin',
            config: {
                title: 'admin',
                templateUrl: 'app/admin/admin.html',
                settings: {
                    nav: 6,
                    content: "<i class='fa fa-dashboard'></i> GUIGUI's Homepage"
                }
            }
        }, {
            url: '/register',
            config: {
                title: 'register',
                templateUrl: 'app/register/register.html',
                settings: {
                    nav: 4,
                    content: '<i class="fa fa-lock"></i> Register'
                },
                visible: false
            }
        }, {
            url: '/login',
            config: {
                title: 'login',
                templateUrl: 'app/login/login.html',
                settings: {
                    nav: 5,
                    content: '<i class="fa fa-lock"></i> Login'
                }
            }
        }, {
            url: '/',
            config: {
                title: 'Client',
                templateUrl: 'app/client/client.html',
                settings: {
                    nav: 1,
                    content: '<i class="fa fa-dashboard"></i> client'
                }
            }
        }, {
            url: '/mission',
            config: {
                title: 'Mission',
                templateUrl: 'app/mission/mission.html',
                settings: {
                    nav: 7,
                    content: '<i class="fa fa-lock"></i> mission'
                }
            }
        }
        , {
            url: '/uploadFile',
            config: {
                title: 'uploadFile',
                templateUrl: 'app/uploadFile/uploadFile.html',
                settings: {
                    nav: 8,
                    content: '<i class="fa fa-lock"></i> uploadFile'
                }
            }
        }
    ];

    return rts;
}

(the app starts with Client ). (该应用程序以Client开头)。

I've created an Authentication service to know the current user (if there's one logged in). 我已经创建了一个身份验证服务来了解当前用户(如果有一个已登录的用户)。

My question is : where would it be the more judicious to update the menu ? 我的问题是:更新菜单在哪里更明智?

I think config.route.js is loaded once at the start. 我认为config.route.js在一开始就加载一次。

My purpose is to add or remove items to the menu according the user is logged in or not, possible ? 我的目的是根据用户是否登录来向菜单添加或删除项目,可能吗?

Where to inject the authenticationService to update the menu ? 在哪里注入authenticationService来更新菜单?

I found a solution, rather simple. 我找到了一个解决方案,相当简单。

Instead of adding or removing items from the menu ( routes , that is I think impossible), it's simpler to play on their visibility, you can add properties to the items of the menu (visible, index, ..., whatever you want), for instance you can do this (it is in config.route.js ): 与其在菜单中添加或删除项目( 路由 ,我认为这是不可能的),不如在其可见性上播放,您可以向菜单项添加属性(可见,索引,...,随便什么) ,例如,您可以执行此操作(位于config.route.js中 ):

        var persistantRoutes = [
            {
                url: '/await',
                config: {
                    title: '',
                    templateUrl: 'app/await/await.html',
                    settings: {
                        nav: 1,
                        content: "",
                        index: 0,
                        visible: false
                    }
                }
            }, {
                url: '/reinit',
                config: {
                    title: '',
                    templateUrl: 'app/reinit/reinit.html',
                    settings: {
                        nav: 2,
                        content: "",
                        index: 0,
                        visible: false
                    }
                }
            }, {
    ...

routes = persistantRoutes;

In the viewmodel of the menu (in my case : sidebar.js ), you can do this : 在菜单的视图模型中(以我为例sidebar.js ),您可以执行以下操作:

function activate() {
    authenticationService.checkAuthentication().then(getNavRoutes);
}

function getNavRoutes() {
    vm.navRoutes = routes.filter(function (r) {

        if (authenticationService.currentUser.isAuthenticated) {
            switch (r.config.title) {
                case "login":
                case "register":
                    r.config.settings.visible = false;
                    break;
                case "userHomePage":
                    r.url = "/";
                    r.config.settings.visible = true;
                    r.config.settings.nav = 3;
                    break;
                case "FGHomePage":
                    r.url = "/FGHomePage";
                    r.config.settings.nav = 4;
                    break;
            }
        }
        else {
            switch (r.config.title) {
                case "login":
                case "register":
                    r.config.settings.visible = true;
                    break;
                case "userHomePage":
                    r.url = "/userHomePage";
                    r.config.settings.visible = false;
                    r.config.settings.nav = 4;
                    break;
                case "FGHomePage":
                    r.url = "/";
                    r.config.settings.nav = 3;
                    break;
            }
        }

        if (r.config.settings.visible == true) {
            return r.config.settings && r.config.settings.nav;
        }
    }).sort(function (r1, r2) {
        return r1.config.settings.nav - r2.config.settings.nav;
    });
}

I have done a service : authenticationService, that allows you to know if the current user is authenticated, knowing this you can render or not the items of the menu. 我已经完成了一项服务:authenticationService,该服务使您可以知道当前用户是否已通过身份验证,知道您可以呈现菜单项。

That is I think rather simple. 我认为这很简单。

Regards. 问候。

{
        url: '/login',
        config: {
            title: 'login',
            templateUrl: 'app/login/login.html',
            settings: {
            }
        }
    }

If you remove nav (you can remove also content) from settings your problem is fixed. 如果从设置中删除导航(也可以删除内容),则问题已得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM