Skip to content

Instantly share code, notes, and snippets.

@beshur
Created December 11, 2015 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beshur/ceb7430722196d2797a5 to your computer and use it in GitHub Desktop.
Save beshur/ceb7430722196d2797a5 to your computer and use it in GitHub Desktop.
javascript microRouter
// 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