【问题标题】:MeteorJS, iron-router, filters, redirection, delayMeteorJS,铁路由器,过滤器,重定向,延迟
【发布时间】:2014-12-13 23:27:19
【问题描述】:
Router._filters = {

  isLoggedOut: function (pause) {
    if (!Meteor.userId()) {
      return this.redirect('home');
    } else {
      this.next();
    }
  },

  isLoggedIn: function (pause) {
    if (Meteor.userId()) {
      return this.redirect('dashboard');
    } else {
      this.next();
    }
  }

};

filters = Router._filters;

Router.onBeforeAction(filters.isLoggedOut, { except: [
  'home'
]});

Router.onBeforeAction(filters.isLoggedIn, { only: [
  'home'
]});

我想在 Iron-router 中制作过滤器,当用户未登录时重定向到“主页”,如果用户登录,则重定向到“仪表板”。一切正常,但在第一种情况下显示仪表板一秒钟,然后重定向完成。我怎样才能摆脱这种延迟?

【问题讨论】:

    标签: meteor delay iron-router


    【解决方案1】:

    我会创建一个自定义控制器。

    // Global Router configuration
    Router.configure({
        loadingTemplate: 'loading'
        notFoundTemplate: 'notFound'
        controller: BaseController
    });
    

    上面的代码说:“对于所有现有的路径,使用BaseController”

    你的 BaseController 应该是这样的

    BaseController = RouteController.extend({
      action: function() {
        if(!Meteor.userId()) {
          // this.render('home');
          // this.go('home);
          this.redirect('home');
        }
        else {
          // this.render('dashboard');
          // this.go('dashboard');
          this.redirect('dashboard');
        }
      }
    })
    

    如果您需要对每个站点进行更多控制,请为您的特定站点提供一个控制器,该控制器继承自 BaseController。它应该是这样的

    // Global Router configuration
    // Remove the controller property!
    Router.configure({
        loadingTemplate: 'loading'
        notFoundTemplate: 'notFound'
    });
    
    Router.map(function() {
        this.route('contact', {
            path: '/contact',
            controller: 'ContactController'
        })
    });
    

    您的 ContactController 应如下所示

    ContactController = BaseController.extend({
        // Do something with this given route,
        // e.g., give it a name property or so
        name: 'contact',
        action: function() {
    
        }
    })
    

    有关更多信息,请查看 Iron.Router 指南。 https://github.com/EventedMind/iron-router/blob/devel/Guide.md#route-controllers

    【讨论】:

    • 我已经这样做了,但仍然有延迟:action: function () { if (!Meteor.userId()) { this.redirect('home'); } 其他 { this.render(); } }
    • “延迟”是什么意思? dashboard 还在显示吗?
    • 我的意思是仪表板显示一秒钟,然后显示主页。
    • 我不知道这是否重要,但我已经在 Router.configure(); layoutTemplate: 'Layout' 可能只有它在显示。
    • 当然。 pastebin.com/G0QbCGHr 。这是修改后的版本,但“布局”仍会显示一段时间。
    猜你喜欢
    • 2015-11-29
    • 2014-01-26
    • 1970-01-01
    • 2016-05-14
    • 2014-12-24
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多