带铁的全球挂钩:路由器v1.0

时间:2014-11-06 16:35:37

标签: meteor iron-router

我正在尝试创建一个检查用户是否已登录的全局挂钩,如果是,检查" userData"出版物完全被发送出去了。 这是因为我的很多代码都依赖于这个发布,我想防止获取未定义的对象(模板在有必要的数据之前呈现)。

以下是在所有路由上声明此默认行为的正确方法吗?

ApplicationController = RouteController.extend({
layoutTemplate: 'appBody',

loadingTemplate: 'appLoading',

onBeforeAction: function () {
 if (!Meteor.user()) {
   this.render('login');
 } else {
   this.next();
 }
},
waitOn: function(){
 return Meteor.subscribe('userData');
},
 action: function () {
  if (this.ready()) //if subscription handle is ready, continue
  this.next();
  else  // otherwise show appLoading template
  this.render('appLoading');
   }
 });

全局配置:

Router.configure({
controller: 'ApplicationController'
});

server.js 需要客户始终订阅' userData'如果他们已登录。

Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId},
    {fields: {'somefield': 1}});
});

如果我要在waitOn函数中订阅单个路线上的其他出版物,它是否会覆盖' userData'出版,还是加入?

0 个答案:

没有答案