Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how meanjs implement admin dashboard

meanjs use angularjs for front end mvc , and is a SPA application , so when the admin dashboard is diffrent than the front end page , what is the best way to implement admin dashboard ? perhaps two access points ?

like image 378
Jeremy Avatar asked Sep 11 '26 19:09

Jeremy


1 Answers

I've been looking for a way to achieve this also and I'm posting here the result of my search. First create your new module:

yo meanjs:angular-module admin
yo meanjs:angular-route adminHome
? Which module does this route belongs to? admin
? What do you want your route path to be? admin-home
? What do you want to call your view? admin-home
? What do you want to call your controller? AdminHome
   create public/modules/admin/config/admin.client.routes.js
   create public/modules/admin/controllers/admin-home.client.controller.js
   create public/modules/admin/tests/admin-home.client.controller.test.js
   create public/modules/admin/views/admin-home.client.view.html

Once you did that first step is to check that when you go on : http://localhost:3000/#!/admin-home you effectively load the generated view. Then you go generating your express route and controller:

yo meanjs:express-route admin
yo meanjs:express-controller admin

Now here come the fun. The idea is to tell Express to load different set of layout views when you're hitting /admin

//Content of app/controllers/admin.server.controller.js
'use strict';

exports.index = function(req, res){
    res.render('admin-index', {
        user: req.user || null,
        request: req
    });
};

//Content of app/routes/admin.server.routes.js
'use strict';

module.exports = function(app) {
    var admin = require('../../app/controllers/admin.server.controller');
    app.route('/admin').get(admin.index);
};

Copy/paste:

app/views/index.server.view.html -> app/views/admin-index.server.view.html
app/views/layout.server.view.html ->app/views/admin-layout.server.view.html

Edit app/views/admin-index.server.view.html:

{% extends 'admin-layout.server.view.html' %}

{% block content %}
    <section data-ui-view></section>
{% endblock %}

Edit app/views/admin-layout.server.view.html:

[...]
{% for jsFile in jsFiles %}<script type="text/javascript" src="/{{jsFile}}"></script>{% endfor %}
[...]

Notice the '/' before {{jsFile}}

Final touch. Because your browser is now loading /admin instead / you need to tell ui-router that the path to the templates are absolute and not relative.

//Content of public/modules/admin/config/admin.client.routes.js
'use strict';

//Setting up route
angular.module('admin').config(['$stateProvider',
    function($stateProvider) {
        // Admin state routing
        $stateProvider.
        state('admin-home', {
            url: '/admin-home',
            templateUrl: '/modules/admin/views/admin-home.client.view.html'
        });
    }
]);

Notice the '/' at the begining of templateUrl content

Now test your admin zone by going to http://localhost:3000/admin/#!/admin-home

like image 123
MaxouMask Avatar answered Sep 14 '26 08:09

MaxouMask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!