Skip to content

Instantly share code, notes, and snippets.

@mikeywaites
Created September 5, 2012 15:47
Show Gist options
  • Save mikeywaites/3638783 to your computer and use it in GitHub Desktop.
Save mikeywaites/3638783 to your computer and use it in GitHub Desktop.
define(
[
'backbone',
'routers/router',
'store'
],
function (Backbone, Router, store) {
$(function() {
new Router();
Backbone.history.start({pushState: true});
if (Backbone.history && Backbone.history._hasPushState) {
// Use delegation to avoid initial DOM selection and allow all matching elements to bubble
$(document).delegate("a", "click", function(evt) {
// Get the anchor href and protcol
var href = $(this).attr("href");
var protocol = this.protocol + "//";
// Ensure the protocol is not part of URL, meaning its relative.
// Stop the event bubbling to ensure the link will not cause a page refresh.
if (href.slice(0, protocol.length) !== protocol && href != '#') {
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);
}
});
}
});
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment