UMDRouter is a lightweight, dependency-free JavaScript library that provides developers with an easy-to-use mechanism for defining dynamic "routes" based on hashbang and / or push-state history. It can be used in place of heaver solutions such as Sammy.js.
First, create an instance of the router:
var router = new UMDRouter();Once your router is instantiated you can build routes via the on() method. This
method supports two formats:
A single callback can be provided which will fire when the route is matched:
router.on("/some/route", function () {
// ... do something ...
});The router will process an object with at least one of the following:
router.on("/some/route/", {
before: function (fn) {
// ... do something before firing the route handler ...
// Continue:
fn(true);
// Break:
fn(false);
},
load: function () {
// ... do something when the route is matched ...
},
unload: function () {
// ... do something when the route is navigated away from ...
}
});The router watches for and matches routes with the :variable convention, for example:
router.on("/some/route/:id", function (id) {
// ... {id} contains the route ID variable
});These parameters function the same in the simple, callback method as well as the object-event structure.
The router is made to allow for extensibility independent of a specific route:
UMDRouter.extend({
"sayCheese": function() {
console.log("Cheese!");
}
});Or by extending inside the route itself:
router.on("/profile", {
load: function () {
this.sayCheese();
},
extend: {
sayCheese: function() {
console.log('Cheese!');
this.derp();
}
}
});$ bower install umdrouter
$ npm install umdrouter