Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jongacnik/1b7245118d752beee690 to your computer and use it in GitHub Desktop.
Save jongacnik/1b7245118d752beee690 to your computer and use it in GitHub Desktop.
Route Mapping Module

Route Mapping Module

Small regex based router (maybe route checker is a better name) whose main purpose is just to map routes to functions. Built with the intention of using with jQuery pjax, so on pjax:complete check to see if the new route has specific code that needs to be executed. If you don't want to use with pjax, just leave the event option blank (or plop in any event you want the router to be bound to). There's jQuery dependency in here, but could be rejiggered to be without pretty easy.

Check out the router.usage.js file for config.

Regex matching logic from miniroutes!

// Router
Modules.Router = function(options) {
var self = {
options : $.extend({
routes : {},
ignore : null,
event : null,
notFound : function() {}
}, options),
events : function() {
if(self.options.event){
$(window).on(self.options.event, function() {
self.navigate();
});
}
},
navigate : function() {
var url = self.getCurrentRoute();
var params;
var cb;
Object.keys(self.options.routes).some(function(i) {
params = self.matches(self.options.routes[i].pattern, url);
if (params !== null) {
cb = self.options.routes[i].cb;
return true;
}
});
$(window).trigger({
type: "route.change",
url: url
});
cb = cb ? cb : self.options.notfound;
return cb(params);
},
matches : function(re, path) {
var matches = re.exec(path);
if (matches === null) return null;
matches = matches.slice(1);
matches = matches.map(function(val) {
if (typeof val === 'undefined') return null;
return val;
});
return matches;
},
getCurrentRoute : function() {
var origin = window.location.origin;
var path = window.location.href;
var pathArray = path.replace(origin,'').split( '/' );
var builtPath = self.options.ignore ? pathArray.join("/").replace(self.options.ignore, '') : pathArray.join("/");
if (builtPath.indexOf('/') === 0)
builtPath = builtPath.replace('/','');
return builtPath;
}
};
self.navigate();
self.events();
return false;
};
var myRouter = Modules.Router({
routes : {
about : {
pattern : /^about\/?$/,
cb : function(params){
// do stuff on about page
}
},
contact : {
pattern : /^contact\/?$/,
cb : function(params){
// do stuff on contact page
}
},
project : {
pattern : /^project(?:\/(.+))?$/,
cb : function(params){
// do stuff on project pages
}
},
home : {
pattern : /^\/?$/,
cb : function(params){
// do stuff on homepage
}
}
},
ignore : '/local/dev/path', // helpful if your root isn't the root
event : 'popstate pjax:complete', // really just made this thing to combine with pjax
notfound : function(){
console.log('not found!');
// do stuff on an unmatched route
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment