Created
November 5, 2012 16:31
-
-
Save francescoagati/4018135 to your computer and use it in GitHub Desktop.
wrapping routie with opal
This file contains hidden or 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
# library wrapped https://github.com/jgallen23/routie | |
class Router | |
attr_accessor :params | |
def initialize | |
@routes={} | |
@params={} | |
end | |
def define(&blk) | |
instance_eval(&blk) | |
end | |
def hash=(url) | |
`window.hash=#{url}` | |
end | |
def hash | |
`window.location.hash` | |
end | |
def page(path,&blk) | |
context=self | |
%x{ | |
routie(#{path},function() { | |
#{context.params=Hash.from_native(`this.params`)}; | |
#{context.instance_eval(&blk)}; | |
}); | |
} | |
end | |
def redirect(path) | |
`routie(#{path})` | |
end | |
alias goto redirect | |
end | |
router=Router.new | |
router.define do | |
page('users/name/:name') { goto 'users/4' } | |
page('users/:id') { puts params } | |
end | |
router.goto('users/name/francesco') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment