Search code examples
mongodbmeteoriron-router

Iron router routes from db


I try to load my routes from db. My collection schema for routes is:

AsideMenu.attachSchema(
new SimpleSchema({
    name: {
        type: String,
        denyUpdate: true
    },
    path: {
        type: String,
        denyUpdate: true
    },
    controller: {
        type: String,
        denyUpdate: true
    }
})

);

I defined also a method for publish and subscribe and everything works well, i can get on client all the records on which he has access, but i cant get the router to register them.

    _.map(menuRoutes, function (route) {
    Router.route(route.path,
        {
            name: route.name,
            controller: route.controller,
        })
})

When I access in client console:

console.log(Router.routes) I get []

And if i print it in server console i get all routes:

I20150227-19:02:47.753(2)?   { [Function]
I20150227-19:02:47.753(2)?     getName: [Function],
I20150227-19:02:47.753(2)?     findControllerConstructor: [Function],
I20150227-19:02:47.754(2)?     createController: [Function],
I20150227-19:02:47.754(2)?     setControllerParams: [Function],
I20150227-19:02:47.754(2)?     dispatch: [Function],
I20150227-19:02:47.754(2)?     path: [Function],
I20150227-19:02:47.754(2)?     url: [Function],
I20150227-19:02:47.755(2)?     params: [Function],
I20150227-19:02:47.755(2)?     get: [Function],
I20150227-19:02:47.755(2)?     post: [Function],
I20150227-19:02:47.755(2)?     put: [Function],
I20150227-19:02:47.755(2)?     delete: [Function],
I20150227-19:02:47.756(2)?     options: 
I20150227-19:02:47.756(2)?      { name: 'calendar.index',
I20150227-19:02:47.756(2)?        controller: 'CalendarController',
I20150227-19:02:47.756(2)?        data: [Function],
I20150227-19:02:47.756(2)?        mount: false },
I20150227-19:02:47.757(2)?     _actionStack: { _stack: [], length: 0 },
I20150227-19:02:47.757(2)?     _beforeStack: { _stack: [], length: 0 },
I20150227-19:02:47.757(2)?     _afterStack: { _stack: [], length: 0 },
I20150227-19:02:47.757(2)?     _methods: {},
I20150227-19:02:47.758(2)?     _path: '/calendar',
I20150227-19:02:47.758(2)?     handler: 
I20150227-19:02:47.758(2)?      { options: [Object],
I20150227-19:02:47.758(2)?        mount: false,
I20150227-19:02:47.758(2)?        method: false,
I20150227-19:02:47.759(2)?        where: 'client',
I20150227-19:02:47.759(2)?        name: 'calendar.index',
I20150227-19:02:47.759(2)?        path: '/calendar',
I20150227-19:02:47.759(2)?        compiledUrl: [Object],
I20150227-19:02:47.759(2)?        handle: [Circular],
I20150227-19:02:47.917(2)?        route: [Circular] },

I want to know if this is possible because i couldnt find any example with this aproach.


Solution

  • Yes, this should be possible. I have a similar setup working just fine, the only difference being that I create the routes from a static array, not a db query. But that should make a difference.

    This works for me, when executed during load:

    _.each(routes, function(foo, route) {
        Router.map(function () {
            this.route(route, {
                path: route,
                action: function() { ... }
            });
        });
    });
    

    Where/when are you executing the _.map code you showed?