2013-12-15 84 views
0

我正在使用Jekyll生成一個靜態站點。我正在使用backbone.js做少量路線。與Jekyll一起使用backbone.js路由

假設主頁在localsite.com ......我不確定如何爲以下URL生成路由。

http://localsite.com/hulk & 
http://localsite.com/thor 

廢船雷神將被路由名稱。

和我的Backbone.js的路由器如下:

<script> 
    var AppRouter = Backbone.Router.extend({ 
     routes: { 
      ":name": "nameRoute" // should match http://localsite.com/anything-here 
     } 
    }); 
    // Initiate the router 
    var app_router = new AppRouter; 

    app_router.on('route:nameRoute', function(name) { 
     console.log(name); 
    }) 

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

</script> 

我不能得到這個工作。有什麼我失蹤? 我可以用Jekyll做這個路由嗎?

我傑基爾的_config.yml有

permalink: pretty 

如何獲得這些路線工作?

還有其他建議嗎?我的目標是在github pages上託管時獲得類似的路由。

+0

必須url http://localsite.com/thor,或者它可以是http ://localsite.com#thor? – RustyToms

+0

@RustyToms,該URL只會是localsite.com/thor。 –

回答

0

就路由而言,它非常簡單。第二部分是將被調用的函數的名稱,它應該在路由器中定義。因此,只要改變你的代碼,這一點,它應該工作:

var AppRouter = Backbone.Router.extend({ 
    routes: { 
     ":name": "nameRoute" // should match http://localsite.com/anything-here 
    }, 

    nameRoute: function(name){ 
     console.log(name); 
    } 
}); 
// Initiate the router 
var app_router = new AppRouter; 

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

如果你遇到麻煩pushState的,擺脫骨幹默認主題標籤,如果檢查出這太問題BackboneJS - Router issues - how to get clean URL's(對本地鏈接尤爲重要,你不想繼續從服務器重新加載),這裏是一個網站擺脫他們的例子:#http://artsy.github.io/blog/2012/06/25/replacing-hashbang-routes-with-pushstate/