【发布时间】:2015-02-21 17:10:12
【问题描述】:
我有 Meteor Roles 包,我正在尝试定义一个管理路由:
var requireLogin = function() {
if (! Meteor.user()) {
debugger // #1
if (Meteor.loggingIn()) {
this.render(this.loadingTemplate);
} else {
console.log("no user");
this.render('AdminLogin');
}
} else {
this.next();
}
};
Router.onBeforeAction(requireLogin, {only: ['AdminMain']});
Router.route('/admin', {
name: 'AdminMain',
layoutTemplate: 'AdminLayout',
waitOn: function(){
debugger // #2
return [
Meteor.subscribe("appointments")
]
}
});
我在服务器/出版物中有这个:
Meteor.publish('appointments', function() {
if (Roles.userIsInRole(this.userId, ['assistant','admin'])) {
return Appointments.find();
} else {
console.log("no user");
return [];
}
});
第一个调试器是waitOn 中的调试器#2。为什么?我为该路线精确指定了OnBeforeAction。根据 Iron Router 指南,当用户导航到“/admin”时,我们的 onBeforeAction 钩子函数将在我们的路由函数之前运行。如果用户没有登录,路由函数永远不会被调用,AdminPage 也不会渲染到页面。
好吧,考虑到调试器首先停止等待 Meteor 订阅,看起来好像在 OnBeforeAction 之前调用了路由函数。由于此订阅需要管理员用户登录服务器,如果我在调试器上按继续,服务器控制台会记录“无用户”,并且加载屏幕会永远持续下去。 requireLogin 的实际 OnBeforeAction 函数永远不会被调用。
【问题讨论】:
标签: meteor iron-router