Created
July 27, 2011 16:28
-
-
Save millermedeiros/1109742 to your computer and use it in GitHub Desktop.
Using hasher together with crossroads.js
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>example crossroads + hasher</title> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
</head> | |
<body> | |
<div id="wrapper"> | |
<ul> | |
<li> | |
<a href="#/home">home</a> | |
</li> | |
<li> | |
<a href="#/lorem">lorem</a> | |
</li> | |
<li> | |
<a href="#/lorem/ipsum">lorem/ipsum</a> | |
</li> | |
</ul> | |
</div> | |
<!-- | |
required scripts: | |
* js-signals (http://millermedeiros.github.com/js-signals/) | |
* crossroads.js (http://millermedeiros.github.com/crossroads.js/) | |
* hasher (https://github.com/millermedeiros/hasher/) | |
* sample (https://gist.github.com/1109742) | |
--> | |
<script src="signals.js"></script> | |
<script src="crossroads.js"></script> | |
<script src="hasher.js"></script> | |
<script src="sample-hasher_crossroads.js"></script> | |
</body> | |
</html> |
This file contains hidden or 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 DEFAULT_HASH = 'home'; | |
//setup crossroads | |
crossroads.addRoute('home'); | |
crossroads.addRoute('lorem'); | |
crossroads.addRoute('lorem/ipsum'); | |
crossroads.routed.add(console.log, console); //log all routes | |
//setup hasher | |
//only required if you want to set a default value | |
if(! hasher.getHash()){ | |
hasher.setHash(DEFAULT_HASH); | |
} | |
function parseHash(newHash, oldHash){ | |
// second parameter of crossroads.parse() is the "defaultArguments" and should be an array | |
// so we ignore the "oldHash" argument to avoid issues. | |
crossroads.parse(newHash); | |
} | |
hasher.initialized.add(parseHash); //parse initial hash | |
hasher.changed.add(parseHash); //parse hash changes | |
hasher.init(); //start listening for hash changes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@amicns I know this was posted almost a year ago, but if anyone else is running into the same problem here's what I did: