2014-10-17 76 views
1

我正在嘗試爲用戶提供一個公共功能,在登錄後將其重定向到最初請求的位於安全路徑後面的url。例如,用戶點擊通過系統通知觸發電子郵件鏈接,試圖去: https://mysite.com/secure/notifications/1 用戶不這麼踢回 https://mysite.com/login 登錄登錄他們應該沒有引起他們的主頁後,但在最初請求的網址。使用rails登錄骨幹路由後重定向

我很熟悉在重定向到登錄頁面之前將嘗試的URL存儲在會話中的技巧。問題是如果URL在覈心URL之後包含骨幹路由器,即 https://mysite.com/secure/notifications/1#details

URL的#details部分不會被髮送到服務器,因爲這通常用於內部頁面跳轉。我想知道Web開發人員如何處理這種JS MVC框架(如骨幹,角度和其他)正在出現?一些詭計?有什麼方法可以在http規範中實際將#傳遞給服務器?

任何想法表示讚賞,謝謝。

回答

1

這個問題的最簡單的方法,如果你不需要支持這種行爲對舊版瀏覽器,是使pushState的在你的骨幹路由器,這樣你就不會爲路由使用#

Backbone.history.state({pushState: true});

編輯:

的其他潛在解決方案,但它是一個有點亂,是做一些URL蠢事弄清楚應該是哈希後什麼,然後導航到該路線。

例如,假設您想瀏覽到:

http://webapp.com/abc/#page1其中「第1頁」是它構成了主幹航線的片段。

如果您將用戶發送至http://webapp.com/abc/page1。您可以檢測瀏覽器是否具有pushState。如果沒有,你可以用散列替換'root'後面的所有內容。下面是一些示例代碼,可能讓你在正確的軌道上,同時支持臺瀏覽器:

var _defaults = { 
 
     pushState: Modernizr.history, 
 
     silent: true, 
 
     root: '/' 
 
    }; 
 

 
    var start = function(options) { 
 
     // Start the routing either with pushstate or without 
 
     options = _.extend(_.clone(this._defaults), options); 
 
     Backbone.history.start(options); 
 
     if (options.pushState) { 
 
     Backbone.history.loadUrl(Backbone.history.getFragment()); 
 
     return; 
 
     } 
 
     this.degradeToNonHistoryURL(); 
 
    }; 
 

 
    /** 
 
    * For fragment URLs, we check if the actual request is for the root i.e '/', 
 
    * If it is, we can continue and Backbone will do the magic 
 
    * If it isn't we redirect to the root with the route as a fragment 
 
    * foo.com/bar/1 -> foo.com/#bar/1 
 
    */ 
 
    degradeToNonHistoryURL = function() { 
 
     var pathName = window.location.pathname; 
 

 
     // If the root is '/', length is one. If the root is 'foo', length is 5 (/foo/) 
 
     var rootLength = _getRoot().length; 
 
     var isRootRequest = pathName.length === rootLength; 
 
     if (!isRootRequest) { 
 
      var route = pathName.substr(rootLength); 
 
      window.location.href = _getRoot() + '#' + route + window.location.search; 
 
      return; 
 
     } 
 
     Backbone.history.loadUrl(Backbone.history.getFragment()); 
 
     }, 
 

 
     /** 
 
     * Get the effective root of the app. Normally it's '/', but if set to 'foo', we want 
 
     * to return '/foo/' so we can more easily determine if this is a root request or not. 
 
     * @returns {String} The effective root 
 
     */ 
 
     _getRoot = function() { 
 
     if (Backbone.history.options.root === '/') { 
 
      return '/'; 
 
     } 
 
     return '/' + Backbone.history.options.root + '/'; 
 
     },

訣竅這裏使pushState的網址您的規範網址,並始終將用戶引導至這些那些。一旦瀏覽器採用增加,理論上應該很容易將所有這些垃圾切除掉,而不必更新所有鏈接。

+0

如果您向未登錄的用戶提供完整的URL路徑,這是如何工作的?網址的一部分根據url參數確定您要瀏覽哪個BB視圖? – bjm88 2014-10-17 20:02:47

+0

這裏有一些文章解釋了爲什麼hash和hashbang標籤不理想。 http://danwebb.net/2011/5/28/it-is-about-the-hashbangs,http://artsy.github.io/blog/2012/06/25/replacing-hashbang-routes-with- pushState的/。 – bjm88 2014-10-23 17:39:16

+0

這是一篇關於理解pushstate的文章,https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history – bjm88 2014-10-23 17:39:57

0

經過一番研究,似乎只有兩種解決方案

  1. 所推薦的意志,使用pushState的並且只支持HTML5的瀏覽器,但是這是使用散列或hashbang的JavaScript導航現有的應用程序上有重大改變。

  2. 服務器端的變通方法,這裏的主要選項是提供重定向端點以獲取用戶需要去的地方。示例 /myapp/redirector?pathroot =通知& hashroot =詳情& hashparam1 = 2 這然後將建立在服務器端的URL /MYAPP /通知/ 1#細節/ 2

所以在#2服務器無法接收HTTP請求與標籤,但它可以發送它們。瀏覽器會收到包含散列導航部分的完整路徑,並執行其正常的JavaScript MVC路由選擇。