2011-11-21 37 views
1

如何設置,可以處理的URL,如骨幹路由器:簡單的Backbone.js路由器? (頁面層次結構+查詢字符串)

example.com/#!/story-1/?a=1&b=2 

或者最好用的子頁面的URL支持:

example.com/#!/chapter-1/story-1/?a=1&b=2 

我基本上想要一個簡單的方法來定義頁面與關聯的查詢字符串。

這是默認支持的,還是我應該使用這個或其他添加? https://github.com/documentcloud/backbone/pull/668

最終結果應該是這樣的:

  1. 請求的資源:
    example.com/#!/chapter-1/story-1/?a=1 & B = 2

  2. 解析,看看它是否在頁面匹配的頁面對象:
    網頁:{ 章-1_story-1:{ 模板:#模板1 } }

  3. 加載頁面模板,頁面控制器與查詢字符串:
    PageController.load(模板,則params)

回答

1

從文檔:

For example, a route of "search/:query/p:page" will match a fragment of 
#search/obama/p2, passing "obama" and "2" to the action. A route of 
"file/*path" will match #file/nested/folder/file.txt, passing 
"nested/folder/file.txt" to the action. 

因此,因此你可以使用像這樣的變量:

routes:{ 
    'search/:query/:page': 'handlePages' 
} 

或這樣的路徑:

routes:{ 
    'search/*path': 'handlePages' 
} 

但我不知道任何查詢字符串處理,但是。

0

您可能需要繪製另一條路線,其中還包括'?'和一個僞網址參數的圖示。例如,這是一個可以擴展Router的函數,它將遍歷路由對象併爲每個查詢字符串繪製一條附加路由。它還會解析url參數並將它們作爲路由方法的最終參數傳遞。

mapRoutesWithParams: function() { 
    _.each(_.keys(this.routes), function(route) { 
    this.route(route+'?*params', // draw the route with a query string 
     this.routes[route], 
     function() { 
     var args = _.toArray(arguments); 
     var params = args.pop(); 
     var paramsObject = _(params.split('&')).reduce(function(memo, pair) { 
      memo[pair.split('=')[0]] = pair.split('=')[1]; return memo; 
     }, {}); 
     return this[this.routes[route]].apply(this, args.concat(paramsObject)); 
     } 
    ); 
    }, this); 
} 
+0

還沒有測試過呢,fyi – maxl0rd