Sha256: b0b8fd166ea83953b7379d49a554da4c0bdec90b2a67b8791cecbb6228a6b9eb

Contents?: true

Size: 1.07 KB

Versions: 10

Compression:

Stored size: 1.07 KB

Contents

class @Router
  constructor: (@pages) ->
    @routes = this.build()
    $(window).bind 'hashchange', => this.draw()

  build: ->
    routes = []
    for pattern, page of @pages
      routes.push route = args: [], page: page, re: null
      if pattern == 'default'
        route.re = pattern
        continue

      fragments = (f for f in pattern.split '/' when f.length > 0)
      map = (fragment) ->
        if fragment[0] == ':'
          route.args.push fragment.replace ':', ''
          '(/[^/]+)?'
        else '/' + fragment
      route.re = new RegExp '#' + (map f for f in fragments).join ''
    routes

  draw: ->
    [route, args] = this.match()
    route ||= this.defaultRoute()
    return unless route
    [opts, ix] = [{}, 0]
    opts[name] = args[ix++] for name in route.args
    route.page.draw(opts)

  match: ->
    for route in @routes
      if match = window.location.hash.match route.re
        args = (arg.replace '/', '' for arg in match[1..-1])
        return [route, args]
    []

  defaultRoute: ->
    for route in @routes
      return route if route.re == 'default'

Version data entries

10 entries across 10 versions & 3 rubygems

Version Path
vines-web-0.1.1 app/assets/javascripts/lib/router.coffee
vines-web-0.1.0 app/assets/javascripts/lib/router.coffee
vines-0.4.6 web/lib/coffeescripts/router.coffee
vinesmod-0.4.5.2 web/lib/coffeescripts/router.coffee
vinesmod-0.4.5 web/lib/coffeescripts/router.coffee
vines-0.4.5 web/lib/coffeescripts/router.coffee
vines-0.4.4 web/lib/coffeescripts/router.coffee
vines-0.4.3 web/lib/coffeescripts/router.coffee
vines-0.4.2 web/lib/coffeescripts/router.coffee
vines-0.4.1 web/lib/coffeescripts/router.coffee