流星角色和铁路由器在一起玩吗?
问题描述:
我有一个只有编辑才能访问的编辑器页面的Meteor应用程序。我正在使用Iron-Router,而我的Router.map如下所示。但是,这不是以一种奇怪的方式工作。如果我提供了一个链接到编辑器页面,那么一切都很好,但是如果我尝试输入/编辑器URL,那么即使用户角色设置正确,它也会始终重定向到主页面。流星角色和铁路由器在一起玩吗?
(有一件事我排除是如果Meteor.userId()不Roles.userIsInRole之前之前设置被调用)。
人知道为什么会是什么?
Router.map(function() {
...
this.route('editor', {
path: '/editor',
waitOn: function() {
//handle subscriptions
},
data: function() {
//handle data
},
before: function() {
if (!Roles.userIsInRole(Meteor.userId(), 'editor')) {
this.redirect('home');
}
}
});
...
});
答
的Roles
包设置了一个automatic publication发送对Meteor.users
集合roles
属性。不幸的是,你无法获得自动出版物的订阅处理,所以你需要自己做。
设置发布用户所需数据的新订阅,然后配置路由器在显示任何页面之前检查数据是否已准备就绪。
如:
if (Meteor.isServer) {
Meteor.publish("user", function() {
return Meteor.users.find({
_id: this.userId
}, {
fields: {
roles: true
}
});
});
}
if (Meteor.isClient) {
var userData = Meteor.subscribe("user");
Router.before(function() {
if (Meteor.userId() == null) {
this.redirect('login');
return;
}
if (!userData.ready()) {
this.render('logingInLoading');
this.stop();
return;
}
this.next(); // Needed for iron:router v1+
}, {
// be sure to exclude the pages where you don't want this check!
except: ['register', 'login', 'reset-password']
});
}
感谢弥敦道。什么是修复? – user592419
哎呀,我已经删除了。 'Remodal'是一种自定义反应模式软件包。 –
这是否有一个非coffescript示例?将不胜感激编辑... –