2011-09-05 24 views

Trả lời

63

Bạn cần phải bật pushState

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

http://backbonejs.org/#Router

http://backbonejs.org/#History

Edit: Như đã đề cập trong các ý kiến ​​này sẽ chỉ làm việc cho các trình duyệt có hỗ trợ pushState, trình duyệt mà không làm sẽ quay trở lại phương thức băm. Không có cách nào thực sự xung quanh điều này, bạn có thể kích hoạt cho trình duyệt hiện đại và rơi trở lại hoặc chỉ sử dụng băm cho tất cả các trình duyệt.

+0

Dưới đây là một số thông tin hơn từ một câu hỏi tương tự [link] (http://stackoverflow.com/a/8280389/706466) –

+7

Cần lưu ý rằng đây là chỉ có sẵn trong các trình duyệt có hỗ trợ cho lịch sử API (trình duyệt hiện đại). Các trình duyệt không có hỗ trợ api lịch sử sẽ sử dụng hàm băm. – Kenzic

10

Backbone Boilerplate có trình trợ giúp tuyệt vời cho phép pushstate. Tôi sử dụng nó khi có những lần tôi muốn bỏ qua bộ định tuyến của mình.

// 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); 
    } 
}); 
Các vấn đề liên quan