Skip to content

Instantly share code, notes, and snippets.

@clalimarmo
Last active September 15, 2015 18:53
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 clalimarmo/364f9ca0e654eb586bb7 to your computer and use it in GitHub Desktop.
Save clalimarmo/364f9ca0e654eb586bb7 to your computer and use it in GitHub Desktop.
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