Skip to content

Instantly share code, notes, and snippets.

@aerykk
Created February 13, 2015 16:31
Show Gist options
  • Save aerykk/04826071ca568bb4c5f9 to your computer and use it in GitHub Desktop.
Save aerykk/04826071ca568bb4c5f9 to your computer and use it in GitHub Desktop.
React Router Pushing State
var dummyLink = document.createElement('a');
var absolutify = function (url) {
dummyLink.href = url;
return dummyLink;
};
$('body').on('click', 'a', function(e) {
var $anchor = $(this);
if (!$anchor.attr('href')) {
return;
}
// Don't process hash changes
if ($anchor.attr('href')[0] === '#') {
return;
}
var parsed = absolutify($anchor.attr('href'));
var href = parsed.href;
var siteUrl = window.location.origin;
if (!href) {
return;
}
if (href.substr(0, siteUrl.length) === siteUrl) {
var path = parsed.pathname;
if (!path) {
return;
}
// Hmmmmm, what to do here?
// History.pushState({}, '', path);
// or router.transitionTo(path.slice(1));
// or ...
router.teardown();
router = ReactRouter.run(Router.routes, path, InitializeRouter);
e.preventDefault();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment