Sto usando il backbone per un progetto corrente. Mi stavo chiedendo se è possibile fare il routing senza hashing #
, come fa davis.js.Percorsi di backbone senza hash?
Grazie!
Sto usando il backbone per un progetto corrente. Mi stavo chiedendo se è possibile fare il routing senza hashing #
, come fa davis.js.Percorsi di backbone senza hash?
Grazie!
È necessario abilitare i pushState
Backbone.history.start({pushState: true})
http://backbonejs.org/#History
Edit: Come notato nei commenti questo funziona solo per i browser che supportano pushState, i browser che non lo fanno ricadrà al metodo hash. Non c'è un modo per aggirare questo, è possibile abilitare per il browser moderno e ricorrere o utilizzare solo hash per tutti i browser.
Backbone Boilerplate ha un eccellente supporto che consente di premere sullo stato. Lo uso quando ci sono volte che voglio bypassare il mio router.
// 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);
}
});
Ecco qualche informazione in più da una domanda simile [link] (http://stackoverflow.com/a/8280389/706466) –
Va notato che questo è disponibile solo nei browser che hanno il supporto per la Storia API (browser moderni). I browser senza il supporto per la storia verranno utilizzati per l'hash. – Kenzic