Last active
August 29, 2015 14:02
-
-
Save samvit/db063f258fa43489ab27 to your computer and use it in GitHub Desktop.
Backbone.Router Hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function initialize_link_click_handler() { | |
// 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) { | |
if (!evt.metaKey) { // So we dont break Command-click. //TODO: make it work for windows ctrl key also | |
// 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 + Utils.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. | |
var route = href.prop.slice(root.length + 1); | |
Backbone.history.navigate(route, true); | |
return false; | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment