Question

Hey guys i am a noob who has just started working on nodejs . I am developing a web application on nodejs using compoundjs.

This is the structure of my view folder

views/
|-- admin
|   |-- games
|   |-- squadplayertypes
|   `-- toursection
|       |-- tourformats
|       |-- tourmatchtypes
|       `-- tours
|-- layouts
|   |-- admin
|   |   `-- toursection

I have created a name space to handle all requests that are directed to "/admin".

Now I want to know if there is away where I can define a common layout for all the routes inside the namespace "admin".

This is how i have written my routes.js

    admin.namespace('toursection', function(toursection){
        toursection.resources('tours',function(tour){
            tour.post('fetchTourDetails','tours#fetchTourDetails',{collection:true});
        });
        toursection.resources('tourmatchtypes');
        toursection.resources('tourformats');
    });
    /*Routes for squad players and all related dependancies*/
    admin.resources('squadplayers');
Was it helpful?

Solution

Unfortunately, it doesn't look like there is currently a way to do this (at least neatly in the namespace area). Layouts are searched in the controller-extensions library, where the controller's name is used to search for a layout, then it defaults on the application layout.

However, it is possible to define one layout to be used between several views. You can use the layout function at the top of a controller to change the layout. For example, I can define the layout app/views/layouts/reports_layout.ejs for the reports namespace.

Example namespace:

map.namespace('reports', function (reports) {
    reports.resources('billing');
});

Controller:

layout('reports');

action('index', function () {
    this.title = 'Billing Report';
    render();
});

OTHER TIPS

Well I believe @making3 has pointed out a standard way to do it. Although there can be a dirty hack that can be done in the application controller if you are sure that the url belonging to your admin section will always have /admin/ in it and such urls will only load the admin layout you can follow the following method and add the below code to your application controller

before('mylayout', function(){
    if(req._parsedUrl.pathname.indexOf('/admin/') != -1)
        layout('admin/admin');
    next();
});

This adds a before handler which checks the current url without the query params and if it contains /admin/ it will set the layout for all the controller actions called with admin namespace routes.

Hope this helps.

EDIT: Correctly as pointed by @guruwinder this will fail if you have configured the routes to accept a param in :param fashion. So please be careful before using this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top