Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created November 5, 2012 16:31
Show Gist options
  • Save francescoagati/4018135 to your computer and use it in GitHub Desktop.
Save francescoagati/4018135 to your computer and use it in GitHub Desktop.
wrapping routie with opal
# 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