Last active
September 15, 2015 18:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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