// 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 + Application.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(); | |
// Note by using Backbone.history.navigate, router events will not be | |
// triggered. If this is a problem, change this to navigate on your | |
// router. | |
Backbone.history.navigate(href, true); | |
} | |
}); |
This comment has been minimized.
This comment has been minimized.
Awesomeness! Thanks for sharing. |
This comment has been minimized.
This comment has been minimized.
Thanks, Tim! I'm using mod_rewrite to get "normal" URLs, so I also check href if it begins with # Look out for javascript: links, too. |
This comment has been minimized.
This comment has been minimized.
Nice tip. Thanks! |
This comment has been minimized.
This comment has been minimized.
Thank you, really cool. |
This comment has been minimized.
This comment has been minimized.
thanks |
This comment has been minimized.
This comment has been minimized.
This doesn't seem to work anymore. |
This comment has been minimized.
This comment has been minimized.
Cool. Doesn't work with anchors though. I just shoved in a
before the protocol check to get it all working. |
This comment has been minimized.
This comment has been minimized.
Thank you very much!!! |
This comment has been minimized.
This comment has been minimized.
Thanks. This is a massive help. |
This comment has been minimized.
This comment has been minimized.
Thanks a lot for this! Works great except if you are holding the cmd button to open links in a new tab |
This comment has been minimized.
This comment has been minimized.
Fixed it here: https://gist.github.com/michaelkoper/9402728 |
This comment has been minimized.
This comment has been minimized.
This gist seems to be close to #1 on Google when you search for backbone router links so here is the Coffeescript version https://gist.github.com/gabceb/9629965 |
This comment has been minimized.
This comment has been minimized.
Updated gist with protection for internal #div hash links, better cross domain link support, more efficient with $document caching, and does not use deprecated "delagate" method from jQuery. |
This comment has been minimized.
This comment has been minimized.
Nice work — this works great! |
This comment has been minimized.
This comment has been minimized.
why use |
This comment has been minimized.
This comment has been minimized.
GitHub never notified me of any of these comments >_> I just updated the Gist to latest version I've been using. |
This comment has been minimized.
This comment has been minimized.
Shouldn't it be |
This comment has been minimized.
This comment has been minimized.
@XemsDoom yes it should You also should probably add a check whether or not someone called preventDefault on the event. |
This comment has been minimized.
Thank you!