Created
December 11, 2015 13:14
-
-
Save beshur/ceb7430722196d2797a5 to your computer and use it in GitHub Desktop.
javascript microRouter
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
// micro Router for Front-end | |
// | |
// Deps: Require.js | |
// | |
// Tries to get the first portion ('folder') of the current location | |
// using Require.js and evaluate it; | |
// | |
// Alex Buznik (shu@buznik.net), 2015 | |
var Router = function() { | |
this.routes = { | |
helpDesk: "./helpDesk", | |
store: "./store", | |
logViewer: "./logViewer", | |
device: "./device", | |
owners: "./owners", | |
lib: "./lib" | |
} | |
this.getPath = function() { | |
var p = document.location.pathname.split("/"); | |
if (p.length) { | |
return p[1]; | |
} else { | |
return false; | |
} | |
} | |
this.initialize = function() { | |
var p = this.getPath(); | |
if (p && typeof this.routes[p] != "undefined") { | |
require([p + '/index'], function(index) { | |
if (index) index(); | |
}); | |
} | |
} | |
this.initialize(); | |
var pub = { | |
initialize: this.initialize, | |
routes: this.routes | |
}; | |
return pub; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment