Created
March 1, 2012 07:40
-
-
Save aerith/1948074 to your computer and use it in GitHub Desktop.
From https://gist.github.com/648950 by yoko
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
dispatcher = | |
location: window.location | |
stash: [] | |
connect: (paths, action) -> | |
if paths or paths is 0 | |
paths = paths.valueOf() | |
# webkit: typeof RegExp is function | |
if typeof paths is 'object' and not paths instanceof RegExp | |
paths = { pathname: paths } | |
@.stash.push [paths, action] | |
@ | |
and: () -> | |
stash = @.stash | |
length = stash.length | |
length[length - 1][2] = true if length > 0 | |
@ | |
dispatch: (location) -> | |
location = if location then location else dispatcher.location | |
stash = @.stash | |
params = {} | |
for data in stash | |
paths = data[0]; action = data[1]; chain = data[2] | |
matched = false | |
for key in paths | |
continue unless paths.hasOwnProperty(key) and paths[key] | |
continue unless location[key] | |
value = paths[key]; path = location[key] | |
result = @.match(value, path) | |
matched = !!result | |
break unless matched | |
params[key] = result | |
if matched | |
action? and action(params) | |
break unless chain | |
@.clear() | |
@ | |
match: (value, path) -> | |
if value instanceof RegExp | |
result = value.exec(path) or false | |
else | |
value = value.toString() | |
result = path.indexOf(value) isnt -1 and value | |
result | |
clear: () -> | |
@.stash = [] | |
@ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment