简体   繁体   English

MEAN.js菜单如何工作?

[英]how does MEAN.js menu work?

In the menus.client.service.js file I'm trying to understand the structure of how the menus in the MEAN.js framework is populated 在menus.client.service.js文件中,我试图理解MEAN.js框架中菜单的填充结构

The code starts out with an empty menus object assigned to this 代码从分配给它的空菜单对象开始

this.menus = {}

and then at the bottom of the file, this.addMenu('topbar') function is called 然后在文件的底部,调用this.addMenu('topbar')函数

    // Add new menu object by menu id
    this.addMenu = function(menuId, isPublic, roles) {
        console.log('pre-this.menus') 
        console.log(this.menus) // empty object

        // Create the new menu
        this.menus[menuId] = {
            isPublic: isPublic || false,
            roles: roles || this.defaultRoles,
            items: [],
            shouldRender: shouldRender
        };
        console.log('post-this.menus')
        console.log(this.menus[menuId]) //complete populated menu with submenus i.e "List Articles", "New Article"

        // Return the menu object
        return this.menus[menuId];
    };

Through this one function, it seems like two other functions are called 通过这一个函数,似乎有两个其他函数被调用

this.addMenuItem and this.addsubMenuItem this.addMenuItem和this.addsubMenuItem

But I don't know how it happened because I don't see them explicitly called in the file. 但我不知道它是怎么发生的,因为我没有看到它们在文件中明确调用。

I think I'm missing an important concept here. 我想我在这里错过了一个重要的概念。 I also looked at the header.client.controller.js file, but all it does is call the getMenu function and assign to $scope.menu 我还查看了header.client.controller.js文件,但它只是调用getMenu函数并分配给$ scope.menu

$scope.menu = Menus.getMenu('topbar');

Here is the full nonfunctional file code 这是完整的非功能性文件代码

jsfiddle: http://jsfiddle.net/4c5gc0aq/ jsfiddle: http//jsfiddle.net/4c5gc0aq/

menus.client.service.js is just the Menus service that is injected in the modules' run blocks. menus.client.service.js只是模块运行块中注入的菜单服务。

Menu items are generated (populated) from within the module configs. 菜单项从模块配置中生成(填充)。 If you look at the articles.client.config.js for example, you will see how the menu gets populated for each module: 例如,如果查看articles.client.config.js,您将看到如何为每个模块填充菜单:

'use strict';

// Configuring the Articles module
angular.module('articles').run(['Menus',
    function(Menus) {
        // Add the articles dropdown item
        Menus.addMenuItem('topbar', {
            title: 'Articles',
            state: 'articles',
            type: 'dropdown'
        });

        // Add the dropdown list item
        Menus.addSubMenuItem('topbar', 'articles', {
            title: 'List Articles',
            state: 'articles.list'
        });

        // Add the dropdown create item
        Menus.addSubMenuItem('topbar', 'articles', {
            title: 'Create Articles',
            state: 'articles.create',
            roles: ['admin']
        });
    }
]);

I got this example from the 0.4.0 version, but I'm sure it's very similar in earlier versions. 我从0.4.0版本中得到了这个例子,但我确信它在早期版本中非常相似。

In the bottom of menus.client.service.js you can also define more menus, like side-menu, or role based menus displayed for users with specific roles (like admins): menus.client.service.js的底部,您还可以为具有特定角色的用户(如管理员)定义更多菜单,如侧边菜单或基于角色的菜单:

this.addMenu('top-admin', {
    isPublic: false,
    roles: ['admin']
});
this.addMenu('top-user', {
    isPublic: false,
    roles: ['user']
});

And then populate them with menu items in the respective module's config (run) block. 然后使用相应模块的config(run)块中的菜单项填充它们。 Eg: 例如:

// Add the articles dropdown item
Menus.addMenuItem('top-admin', {
    title: 'Articles',
    state: 'articles',
    type: 'dropdown'
});

// Add the dropdown list item
Menus.addSubMenuItem('top-admin', 'articles', {
    title: 'List Articles',
    state: 'articles.list'
});

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

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