铁路由器除了失败?

时间:2014-12-15 20:48:53

标签: meteor routing iron-router

所以我刚刚开始使用铁路由器,而且我一直在构建一个登录系统。它在每个路由之前通过.onBeforeAction挂钩工作,检查用户是否已登录。但是,我想要公开一些路由,所以我根据文档添加了一个except选项。除了问题是它不起作用:(任何人都可以看到原因吗?

Router.route('/new', function () {
  name: 'new',
  this.render('newComp');
});

Router.route('/c/:_id', { 
  name: 'compPage',
    data: function() { return Comps.findOne(this.params._id); }
});


Router.route('/c/:_id/embed', function () {
  name: 'embed',
  this.layout('empty'),
  this.render('compEmbed', {
    data: function () {
      return Comps.findOne({_id: this.params._id});
    }
  });
});

function loginFunction(){
  // all properties available in the route function
  // are also available here such as this.params

  if (!Meteor.user()) {
    // if the user is not logged in, render the Login template
    if (Meteor.loggingIn()) {
      this.render(this.loadingTemplate);
    } else {
      this.layout('empty');
      this.render('login');
    }
  } else {
    // otherwise don't hold up the rest of hooks or our route/action function
    this.next();
  }
}

Router.onBeforeAction( loginFunction, { 
  except: ['embed'] // this aint working
});

1 个答案:

答案 0 :(得分:6)

问题似乎在你的路由定义中,名称param应该在Router.route()的第三个参数中,就像这样(所以你的路由实际上没有名字,因此except:['route.name']不起作用):

Router.route('/c/:_id/embed', function () {
  this.layout('empty'),
  this.render('compEmbed', {
    data: function () {
      return Comps.findOne({_id: this.params._id});
    }
  });
}, {
  name: 'embed',
});

有关命名路线的更多信息:http://eventedmind.github.io/iron-router/#named-routes