2011-09-05 7 views
47

私は現在のプロジェクトにバックボーンを使用しています。私はdavis.jsのようにハッシュなしでルーティングを行うことが可能かどうか疑問に思っていた。#ハッシュのないバックボーンルート?

ありがとうございます!コメントで述べたように、これは唯一のpushStateをサポートするブラウザでは動作しますが、いないブラウザ:あなたはpushState

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

http://backbonejs.org/#Router

http://backbonejs.org/#History

編集を有効にする必要があり

答えて

63

ハッシュ法に戻ります。この問題を回避する方法はありません。現代のブラウザを有効にして、すべてのブラウザでハッシュを使用することができます。

+0

はここで同様の質問からいくつかの詳細情報です[リンク](http://stackoverflow.com/a/8280389/706466) –

+7

歴史のためにサポートしているブラウザでのみ利用可能であることに留意すべきですAPI(現代のブラウザ)。ヒストリAPIをサポートしていないブラウザはハッシュを使用します。 – Kenzic

10

Backbone Boilerplateは、プッシュステートを可能にする優れたヘルパーを有する。私はルータをバイパスしたい時があるときにそれを使用します。

// Trigger the initial route and enable HTML5 History API support, set the 
// root folder to '/' by default. Change in app.js. 
Backbone.history.start({ pushState: true, root: app.root }); 

// All navigation that is relative should be passed through the navigate 
// method, to be processed by the router. If the link has a `data-bypass` 
// attribute, bypass the delegation completely. 
$(document).on("click", "a[href]:not([data-bypass])", function(evt) { 
    // Get the absolute anchor href. 
    var href = { prop: $(this).prop("href"), attr: $(this).attr("href") }; 
    // Get the absolute root. 
    var root = location.protocol + "//" + location.host + app.root; 

    // Ensure the root is part of the anchor href, meaning it's relative. 
    if (href.prop.slice(0, root.length) === root) { 
    // Stop the default event to ensure the link will not cause a page 
    // refresh. 
    evt.preventDefault(); 

    // `Backbone.history.navigate` is sufficient for all Routers and will 
    // trigger the correct events. The Router's internal `navigate` method 
    // calls this anyways. The fragment is sliced from the root. 
    Backbone.history.navigate(href.attr, true); 
    } 
}); 
関連する問題