Skip to content

Instantly share code, notes, and snippets.

@verekia
Created January 20, 2016 19:50
Show Gist options
  • Save verekia/e14535c3f36386b59c92 to your computer and use it in GitHub Desktop.
Save verekia/e14535c3f36386b59c92 to your computer and use it in GitHub Desktop.
define(function(){
var routes = [{hash:'#list', controller:'ListController'},
{hash:'#add', controller:'AddController'}];
var defaultRoute = '#list';
var currentHash = '';
function startRouting(){
window.location.hash = window.location.hash || defaultRoute;
setInterval(hashCheck, 100);
}
function hashCheck(){
if (window.location.hash != currentHash){
for (var i = 0, currentRoute; currentRoute = routes[i++];){
if (window.location.hash == currentRoute.hash)
loadController(currentRoute.controller);
}
currentHash = window.location.hash;
}
}
function loadController(controllerName){
require(['Controllers/' + controllerName], function(controller){
controller.start();
});
}
return {
startRouting:startRouting
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment