Skip to content

Instantly share code, notes, and snippets.

@clalimarmo clalimarmo/router.js
Last active Sep 15, 2015

Embed
What would you like to do?
Deep Linking with React + Flux, example Router, to be used by a Store
// Router exposes two methods:
//
// - register binds regex patterns to callbacks,
//
// - route takes a path, and executes the callback associated
// with the first regex that matches that path
Router = function() {
var instance = {};
var routes = [];
instance.register = function(matcher, callback) {
routes.push(Route(matcher, callback));
};
instance.route = function(path) {
for (var i in routes) {
var route = routes[i];
if (route.matches(path)) {
route.execute(path);
return;
}
}
};
return instance;
};
Route = function(matcher, callback) {
var instance;
instance.matches = function(path) {
return path.matches(matcher);
};
instance.execute = function(path) {
callback(path, matcher);
};
return instance;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.