Created
February 13, 2015 16:31
-
-
Save aerykk/04826071ca568bb4c5f9 to your computer and use it in GitHub Desktop.
React Router Pushing State
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
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