来自 db 的 Iron 路由器路由

Iron router routes from db

我尝试从数据库加载我的路线。我的路线收集模式是:

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

);

我还定义了一个发布和订阅方法,一切正常,我可以在客户端上获取他有权访问的所有记录,但我无法让路由器注册它们。

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

当我在客户端控制台访问时:

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

如果我在服务器控制台打印它,我会得到所有路由:

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] },

我想知道这是否可行,因为我找不到任何有关此方法的示例。

是的,这应该是可能的。我有一个类似的设置工作得很好,唯一的区别是我从静态数组而不是数据库查询创建路由。但这应该有所作为。

这对我有用,在加载期间执行时:

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

Where/when 你在执行你显示的 _.map 代码吗?

Iron router 运行反应式计算,但此时您对路由定义集合的订阅尚未准备就绪。因此,如果您将该块放入准备好的回调中,即:

Meteor.subscribe('router-data-publication', function() {
  /* construct your routes here*/
});

或者,您可以做一些更安全但本质上相同的事情,使用 Tracker 中的句柄并检查处理程序上的 .ready()。