meteor - Iron-router 使用钩子(Hook)限制管理员访问

标签 meteor iron-router

我的铁路由器有问题。

当我第一次加载页面/admin/deps 时,路由会正常工作。然后我通过单击链接转到任何其他页面,然后在控制台中返回/admin/deps/我得到“路由调度从未呈现。您是否忘记在 onBeforeAction 中调用 this.next()? "并且模板未呈现

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

这是我的 Controller 和路线:

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);
  }
});

最佳答案

最后,我找到了答案,问题出在DepsController 中,在load() 中。功能。它在 onBeforeAction() 之前触发,也需要调用this.next ()

关于meteor - Iron-router 使用钩子(Hook)限制管理员访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27376480/

相关文章:

ios - 适用于 iOS 的 Cordova 移动应用程序未与服务器通信

javascript - 如何使用MeteorPad? (更新: works at home not at work -- firewall,代理问题?)

javascript - MeteorJS - 访问同一模板的助手

meteor - 如何使用 Iron Router 在 Meteor 中通过 URL 获取路由名称

meteor - 熨斗 :router 1. 0.9 不工作

meteor - 以编程方式单击 Meteor JS 中的 Iron Router 链接

javascript - 如何更新当前用户(帐户)

过期的 meteor 收藏

Meteor:为什么 Iron Router 的 onBeforeAction 会被多次调用?

javascript - 错误: Router is not defined V0. 7.0 meteor 0.8.0