Created
May 18, 2016 05:20
-
-
Save kristian76/91705f0d56bb3da719e5868a17d372d4 to your computer and use it in GitHub Desktop.
SPA RxJS based router
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 hashChangeEvent = Rx.Observable.fromEvent(window, 'hashchange'); | |
var hash = location.hash; | |
var hashSubject = new Rx.BehaviorSubject(hash); | |
hashSubject.subscribe(function(hash) { | |
router(hash.replace('#', '')); | |
}); | |
hashChangeEvent.subscribe(function(e) { | |
hashSubject.onNext(location.hash); | |
}); | |
function router(hash) { | |
console.log('Do magic stuff to %s', hash) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On page load the hash is passed to router(), when ever the hash changes, the new hash is passed to the router. hashChangeEvent also knows the old hash