iron-router使用hook限制管理员访问

时间:2014-12-09 10:18:06

标签: meteor iron-router

我有铁路由器的问题。

当我第一次加载页面/ admin / deps时,路由将运行正常。然后我通过点击链接转到任何其他页面,然后回到/ admin / deps /那时候在控制台中我得到“路由调度从未渲染。你有没有忘记在onBeforeAction中调用this.next()? “和模板不呈现

但是..如果我使用热插拔代码更改代码中的任何内容,流星会重新加载页面,在这种情况下,一切都按预期工作。

这是我的控制器和路线:

class @AdminRouteController extends RouteController
  onBeforeAction: ->
    console.log "onBeforeAction: #{@url}"
    if Meteor.loggingIn()
      @render 'loading'
    else if !Meteor.userId() or !Meteor.user().hasAccess 'admin'
      @render 'notFound'
    else
      @next()

class @AdminPagebleRouteController extends AdminRouteController
  pageable: true 
  perPage: 20    

  limit: (name = null) ->
    Session.get(varName(@, name)) || @perPage

  incLimit: (name = null, inc = null) ->
    inc ||= @perPage
    Session.set varName(@, name), (@limit(name) + inc)

  resetLimit: (name = null) ->
    Session.set varName(@, name), null

  loaded: (name = null) ->
    true

  onRerun: ->
    console.log "onRerun: #{@url}"
    @next()

Router.route '/admin', name: 'admin'
class @AdminController extends AdminRouteController
  template: 'adminHome'
  action: ->
    document.title = i18n 'admin.title'
    super()

Router.route '/admin/deps', name: 'deps'
class @DepsController extends AdminPagebleRouteController
  template: 'deps'
  perPage: 20

  waitOn: ->
    @subscribe 'adminDeps', @limit()

  data: ->
    deps: DepartmentsCollection.find()

  loaded: ->
    @limit() > DepartmentsCollection.find().count()

  action: ->
    document.title = i18n 'deps.title'
    super()

  onRun: ->
    console.log "onRun: #{@url}"
    @resetLimit()
    @next()

  load: ->
    $('html, body').animate({ scrollTop: 0 }, 400)
    $('.content').hide().fadeIn(1000)

这是路由无法正常工作时的控制台屏幕 enter image description here

以下是javascript中的代码:

AdminRouteController = RouteController.extend({
  onBeforeAction: function(){
    console.log("onBeforeAction: " + this.url);
    if(Meteor.loggingIn())
      this.render('loading');
    else if(!Meteor.userId() || !Meteor.user().hasAccess('admin'))
      this.render('notFound');
    else
      this.next();
  }
});

varName = function(inst, name){
  name = name && ("_" + name || "");
  return inst.constructor.name + name + "_limit";
}

AdminPagebleRouteController = AdminRouteController.extend({
  pageable: true,
  perPage: 20,

  limit: function (name){
    return Session.get(varName(this, name)) || this.perPage;
  },
  incLimit: function (name, inc){
    inc = inc ? inc : this.perPage;
    return Session.set(varName(this, name), (this.limit(name) + inc));
  },
  resetLimit: function (name){
    return Session.set(varName(this, name), null);
  },
  loaded: function (name){
    return true;
  },
  onRerun: function (){
    console.log("onRerun: " + this.url);
    this.next();
  }
});

Router.route('/admin', name: 'admin');
AdminController = AdminRouteController.extend({
  template: 'adminHome',
  action: function(){
    document.title = i18n('admin.title');
    super();
  }
});

Router.route('/admin/deps', name: 'deps');
DepsController = AdminPagebleRouteController.extend({
  template: 'deps',
  perPage: 20,

  waitOn: function(){
    this.subscribe('adminDeps', this.limit());
  },
  data: function(){
    return { deps: DepartmentsCollection.find() };
  },
  loaded: function(){
    return this.limit() > DepartmentsCollection.find().count();
  },
  action: function(){
    document.title = i18n('deps.title');
    super();
  },
  onRun: function(){
    console.log("onRun: " + this.url);
    this.resetLimit();
    this.next();
  },
  load: function(){
    $('html, body').animate({ scrollTop: 0 }, 400);
    $('.content').hide().fadeIn(1000);
  }
});

2 个答案:

答案 0 :(得分:1)

最后,我在load()函数中找到了答案,问题出在DepsController中。它在onBeforeAction()之前解雇,并且还需要调用this.next ()

答案 1 :(得分:0)

您可以将代码放入经典的Javascript语法中吗?几乎所有文档都不是您提供的语言,这不会减轻您可以获得的支持。

此外,我会说你的类@AdminRouteController的onBeforeAction可能是错误的。尝试将if / elseif / else语句切成多个,如下所示:

class @AdminRouteController extends RouteController
  onBeforeAction: ->
    console.log "onBeforeAction: #{@url}"
    if Meteor.loggingIn()
      @render 'loading'
    else 
      @next()

    if !Meteor.userId() or !Meteor.user().hasAccess 'admin'
      @render 'notFound'
    else
      @next()

希望这会有所帮助,但我会更乐于使用经典的javascript语法tbh。

修改

将代码放入.js中。 通常,当您收到错误时#34; Route dispatch从未呈现。您是否忘记在onBeforeAction中调用this.next()?"它是因为你忘记在onBeforeAction()或onRun()某处调用this.next()。需要弄明白......

干杯,