【问题标题】:except option not working in Router.onBeforeAction除了选项在 Router.onBeforeAction 中不起作用
【发布时间】:2014-09-13 10:01:25
【问题描述】:

我的routes.coffee 文件中有以下代码,Router.onBeforeAction(... 函数似乎忽略了except 选项并不断重定向到“登录”模板。这是为什么呢?

enforceLogin = ->
    if Meteor.userId() is undefined or Meteor.userId() is null
        isLoggedIn = false
        Router.go '/sign-in'
    else
        isLoggedIn = true
    return isLoggedIn

isCharacterNamed = ->
    userCharacter = Characters.findOne ownedBy: Meteor.userId()
    userCharacter.name is not undefined

checkCharacterNamed = (context) ->
    if !isCharacterNamed()
        Router.go 'create-character'

userCharactersSubscription = -> Meteor.subscribe 'userCharacters'

nonGameRoutes = ['sign-in', 'sign-up', 'sign-out', 'blog']

Router.map ->
    @route 'home', path: '/'
    @route 'adventures'

    @route 'create-character', onBeforeAction: ->
        enforceLogin()
        if @ready()
            if isCharacterNamed() then Router.go 'home'

    @route 'blog'
    @route 'admin/blog'

    @route 'loading'

    @route 'sign-in'
    @route 'sign-up'
    @route 'sign-out'

Router.onBeforeAction('loading')
Router.onBeforeAction enforceLogin, { except: nonGameRoutes }

Router.waitOn(userCharactersSubscription)

Router.onAfterAction ->
    if @ready()
        checkCharacterNamed()
        @render()
    else
        @render('loading')
,
    { except: _.union(nonGameRoutes, ['create-character']) }

【问题讨论】:

  • 这里有类似的问题。我通过自己在enforceLogin 函数中处理期望来回退。似乎除了对我也没有真正的工作。也许这是一个错误。

标签: coffeescript meteor iron-router


【解决方案1】:

尝试`Router.onBeforeAction enforceLogin,除了:nonGameRoutes

【讨论】:

  • 我明天会做,但不会编译成相同的 javascript 吗?
  • 嗯.. 像这样将 js 语法与 coffescript 混合在一起看起来很奇怪。但你可能是对的,这不是问题的原因。您是否为 onBeforeAction("loading") 定义了“loadingTemplate”以正确启动?也许这会导致页面重新加载。尝试将一些 console.log 放入 enforceLogin 以确保它正在启动。
  • 是的,这很正常,大括号和括号是可选的。我确实定义了加载模板,但那里的代码似乎缺少它,我会检查一下。 enforceLogin 有效,因为我通过将它放在每个适当的路由中来临时解决了这个问题。
  • 我并不是说它不起作用。我只是想知道该功能是否真的是重定向的原因。例如,可能只是 Meteor 重新加载了由不存在的路由引起的页面。您在控制台日志中看到什么了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
相关资源
最近更新 更多