2014-12-03 47 views
0

我對所有網頁使用相同的佈局。但是,當用戶登錄時,我的layout.js不再有效,特別是Template.layout.rendered函數。鐵路由器(流星1.0)Template.layout.rendered不適用於私人網頁

我該如何處理?

Router.configure({ 
    templateNameConverter: "upperCamelCase", 
    routeControllerNameConverter: "upperCamelCase", 
    layoutTemplate: "layout", 
    notFoundTemplate: "notFound", 
    loadingTemplate: "loading" 
}); 

var publicRoutes = ["home_public", "home-public", "login", "register", "forgot_password", "reset_password"]; 
var privateRoutes = ["home-private", "orders", "orders.insert", "orders.details", "orders.edit"]; 

Router.ensureLogged = function() { 
    if(!Meteor.user()) { 
     // user is not logged in - redirect to public home 
     this.redirect("home_public"); 
     return; 
    } else { 
     // user is logged in - check role 
     this.next(); 
    } 
}; 

Router.ensureNotLogged = function() { 
    if(Meteor.user()) 
     this.redirect("home_private"); 
    else 
     this.next(); 
}; 

Router.onBeforeAction(Router.ensureNotLogged, {only: publicRoutes}); 
Router.onBeforeAction(Router.ensureLogged, {only: privateRoutes}); 
+0

我有舊版本的鐵路由器和重定向的一些問題。它主要與反應性停止工作有關,只是爲了檢查 - 如果在重定向發生(未)時重新加載瀏覽器,您的代碼是否工作? – 2014-12-03 22:01:47

+0

Btw。你也可以嘗試用Router.go()替換this.redirect()。 – 2014-12-03 22:02:07

+0

不,重新啓動瀏覽器沒有幫助。 – mfr 2014-12-04 00:15:59

回答

0

看來,流星或鐵路由器(我懷疑流星)有一些問題。

**我的修復是利用私人和公共頁面單獨佈局,如layout_privatelayout_public。 **

流星在登錄/註銷之間切換時,不會重新執行Template.layout.render(如果使用相同的佈局),它會以某種方式失去JavaScript事件偵聽器。

+0

我懷疑我的問題與流星在開發環境中運行的方式有關,甚至可能存在?看起來像在登錄時,佈局不會改變是有意義的。但是,當我登錄菜單從它的JavaScript斷開連接,並且Template.layout.rendered不被觸發。 (使用alert()進行測試)但是,當已經登錄(會話)時,啓動應用程序或刷新它並不會觸發Template.layout.rendered。有些東西會斷開jQuery監聽器。它看起來像流星在開發中加載一切,因爲沒有登錄並自動登錄用戶。 – mfr 2014-12-04 00:18:38

+0

希望它可以幫助某人。我花了幾個小時。 – mfr 2014-12-05 23:35:54