javascript - how does MEAN.js menu work? - Stack Overflow

In the menus.client.service.js file I'm trying to understand the structure of how the menus in the

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

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

    // 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]) //plete 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

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

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

Here is the full nonfunctional file code

jsfiddle: /

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

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

    // 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]) //plete 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

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

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

Here is the full nonfunctional file code

jsfiddle: http://jsfiddle/4c5gc0aq/

Share Improve this question edited Feb 14, 2015 at 16:15 abcf asked Feb 14, 2015 at 15:58 abcfabcf 6952 gold badges10 silver badges25 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

menus.client.service.js is just the Menus service that is injected in the modules' run blocks.

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:

'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.

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):

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. 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'
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745180524a4615397.html

相关推荐

  • javascript - how does MEAN.js menu work? - Stack Overflow

    In the menus.client.service.js file I'm trying to understand the structure of how the menus in the

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信