Last active
February 1, 2019 18:27
-
-
Save pdf/8364f1883ec49c54c65b2108f77d2d57 to your computer and use it in GitHub Desktop.
Trivial nested vecty router
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
package main | |
import ( | |
"github.com/gopherjs/vecty" | |
"github.com/gopherjs/vecty/elem" | |
"github.com/gopherjs/vecty/event" | |
"github.com/pdf/vectyx/router" | |
) | |
type App struct { | |
vecty.Core | |
children vecty.ComponentOrHTML | |
} | |
func (a *App) OnRoute(ctx router.Context) { | |
a.children = ctx.Children | |
if ctx.Rendered { | |
vecty.Rerender(a) | |
} | |
} | |
func (a *App) onClick(e *vecty.Event) { | |
router.Go(`/page1`, nil) | |
} | |
func (a *App) Render() *vecty.HTML { | |
return elem.Div( | |
elem.Button(event.Click(a.onClick), vecty.Text(`Page 1`)), | |
a.children, | |
) | |
} | |
type Page1 struct { | |
vecty.Core | |
children vecty.ComponentOrHTML | |
} | |
func (p *Page1) OnRoute(ctx router.Context) { | |
p.children = ctx.Children | |
if ctx.Rendered { | |
vecty.Rerender(p) | |
} | |
} | |
func (p *Page1) onClick(e *vecty.Event) { | |
router.Go(`/page2`, nil) | |
} | |
func (p *Page1) Render() *vecty.HTML { | |
return elem.Div( | |
elem.Button(event.Click(p.onClick), vecty.Text(`Page 2`)), | |
p.children, | |
) | |
} | |
type Page2 struct { | |
vecty.Core | |
children vecty.ComponentOrHTML | |
} | |
func (p *Page2) OnRoute(ctx router.Context) { | |
p.children = ctx.Children | |
if ctx.Rendered { | |
vecty.Rerender(p) | |
} | |
} | |
func (p *Page2) onClick(e *vecty.Event) { | |
router.Go(`/`, nil) | |
} | |
func (p *Page2) Render() *vecty.HTML { | |
return elem.Div( | |
elem.Button(event.Click(p.onClick), vecty.Text(`Home`)), | |
p.children, | |
) | |
} | |
func main() { | |
r := router.New(nil) | |
r.Group(`/`, &App{}, func(r *router.Router) { | |
r.Handle(`/page1`, &Page1{}) | |
r.Handle(`/page2`, &Page2{}) | |
}) | |
vecty.RenderBody(r.Body()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment