Skip to content

Instantly share code, notes, and snippets.

@func09
Created July 16, 2011 17:15
Show Gist options
  • Save func09/1086556 to your computer and use it in GitHub Desktop.
Save func09/1086556 to your computer and use it in GitHub Desktop.
routes.js.coffee
match = (path, callback) ->
$ ->
param_names = path.match(/:([\w]+)/g)
regexp = new RegExp('^'+path.replace(/:[\w]+/g,'([\\w]+)')+'$')
if m = window.location.pathname.match regexp
m.shift()
params = {}
if param_names
for param_name in param_names
params[param_name] = m.shift()
callback.apply this, [params]
window.match = match
# in other file examples
match '/users/:id', (params) ->
App.User.Show.instance().setup(params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment