坚持加载模板

时间:2014-09-07 10:43:01

标签: meteor

嘿,我有一个系统,登录的用户可以参加"战斗"并且一旦它们出现,我希望用户被锁定在给定的模板上,直到战斗结束。

我有

#
# Currently active battle
#
Meteor.publish 'activeBattle', ->
  character = Characters.findOne(userId: this.userId)
  if this.userId and character
    return Battles.find({active: true, $or: [{characterOneId: character._id}, {characterTwoId: character._id}]})
  else
    return

和我的铁路由器

Router.configure
  layoutTemplate: 'layout'
  loadingTemplate: 'loading'
  waitOn: [
    Meteor.subscribe('activeBattle')
  ]

...

redirectToActiveBattle = (pause) ->
  battle = Battles.findOne(active: true)
  if battle and Meteor.userId()
    throwError('You have a battle in progress.')
    Router.go('combat', {_id: battle._id})
    pause()

...

Router.onBeforeAction(redirectToActiveBattle, except: ['login', 'logout', 'signup', 'combat'])

这在用户登录并且有角色时有效,但如果没有,则页面会卡在加载模板上,而不是显示登录屏幕

1 个答案:

答案 0 :(得分:1)

实际上,如果您不想在出版物中返回任何内容,则应使用this.ready();而不是空回报。因此,正确的出版方式可能是:

Meteor.publish 'activeBattle', ->
  character = Characters.findOne(userId: this.userId)
  if this.userId and character
    return Battles.find({active: true, $or: [{characterOneId: character._id}, {characterTwoId: character._id}]})
  this.ready()