Skip to content

Instantly share code, notes, and snippets.

@jeffrydegrande
Created January 17, 2013 16:25

Revisions

  1. jeffrydegrande created this gist Jan 17, 2013.
    18 changes: 18 additions & 0 deletions router.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    class Router
    @prefix = "something"
    @path: (url)-> @prefix + url
    @pathWithArgs: (url, args)-> @prefix + url + @serialize(args)
    @serialize = (obj, prefix) ->
    str = []
    for p, v of obj
    k = if prefix then prefix + "[" + p + "]" else p
    if typeof v == "object"
    str.push(serialize(v, k))
    else
    str.push(encodeURIComponent(k) + "=" + encodeURIComponent(v))

    str.join("&")

    # route definitions
    @people_path: -> @path "/people"
    @person_path: (id, args)-> @pathWithArgs("/person/#{id}?", args)