waypoint
Version:
Waypoint provides browser and server side routing
74 lines (55 loc) • 1.89 kB
text/coffeescript
Route = require('./route')
isArray = Array.isArray
isArray or= ((obj) -> Object.prototype.toString.call(obj) is '[object Array]')
class Router
constructor: (config) ->
if config
for key in ['routes', 'baseUri', 'notFound', 'multi']
@[key] = config[key] if config[key]?
routeMap = config.routeMap if config.routeMap?
or= []
or= ''
or= no
if routeMap?
route: (method, uri, callback) ->
if typeof method is 'object'
route = method
else
route = new Route(method, uri, callback)
.push(route)
return @
get : (uri, callback) ->
post: (uri, callback) ->
extractUriAndMethod = (uri) ->
matches = uri.match(/^(GET|POST) (.+)/)
if matches and matches.length?
[uri, method] = matches[1..2].reverse()
method or= 'GET'
return [uri, method]
else
return [uri or '', 'GET']
routeMap: (map, baseUri = ) ->
for uri, callback of map
[uri, method] = extractUriAndMethod(uri)
uri = baseUri+uri
if typeof callback is 'function' or isArray(callback)
else if typeof callback is 'object'
else
throw 'Map must be string array or object'
return @
dispatch: (method, uri, scope = {}) ->
hasMatched = no
for route in
continue unless matches = route.match(method, uri)
hasMatched = yes
if isArray(route.callback)
callbacks = route.callback
else
callbacks = [route.callback]
c.apply(scope, matches) for c in callbacks
break unless
.call(scope) if ? and hasMatched is no
return hasMatched
module.exports = Router