2014-12-13 61 views
0
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' 
]}); 

我想使鐵路由器過濾器,這使得重定向到「家」時和「儀表盤」,如果用戶登錄未登錄用戶英寸一切正常,但在第一種情況下儀表板顯示一秒鐘,然後重定向完成。我怎樣才能擺脫這種延遲?MeteorJS,鐵路由器,過濾器,重定向,延遲

回答

0

我會創建一個自定義控制器。

// 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

+0

我已經這樣做了,但仍然有延遲: 行動:(!Meteor.userId())函數(){ 如果{ this.redirect( '家'); } else { this.render(); } } – tdudzik 2014-12-13 17:56:48

+0

你是什麼意思與「延遲」? 「儀表板」仍然顯示? – Ron 2014-12-13 18:07:24

+0

我的意思是儀表板顯示一秒鐘,然後顯示主頁。 – tdudzik 2014-12-13 18:16:35