9
function(app, Router) {
11
// Define your master router on the application namespace and trigger all
12
// navigation from this instance.
13
app.router = new Router();
14
// Trigger the initial route and enable HTML5 History API support, set the
15
// root folder to '/' by default. Change in app.js.
16
Backbone.history.start({ pushState: false, root: app.root });
17
// All navigation that is relative should be passed through the navigate
18
// method, to be processed by the router. If the link has a `data-bypass`
19
// attribute, bypass the delegation completely.
20
$(document).on("click", "a:not([data-bypass])", function(evt) {
21
// Get the absolute anchor href.
22
var href = { prop: $(this).prop("href"), attr: $(this).attr("href") };
23
// Get the absolute root.
24
var root = location.protocol + "//" + location.host + app.root;
25
// Ensure the root is part of the anchor href, meaning it's relative.
26
if (href.prop && href.prop.slice(0, root.length) === root) {
27
// Stop the default event to ensure the link will not cause a page
31
// `Backbone.history.navigate` is sufficient for all Routers and will
32
// trigger the correct events. The Router's internal `navigate` method
33
// calls this anyways. The fragment is sliced from the root.
34
Backbone.history.navigate(href.attr, true);