refactor: move cation trigger logic to its own file; separate conter and greeter examples
This commit is contained in:
18
actiontrigger.go
Normal file
18
actiontrigger.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package via
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-via/via/h"
|
||||
)
|
||||
|
||||
// actionTrigger represents a trigger to an event handler fn
|
||||
type actionTrigger struct {
|
||||
id string
|
||||
}
|
||||
|
||||
// OnClick returns a via.h DOM node that triggers on click. It can be added
|
||||
// to other nodes in a view.
|
||||
func (a *actionTrigger) OnClick() h.H {
|
||||
return h.Data("on:click", fmt.Sprintf("@get('/_action/%s')", a.id))
|
||||
}
|
||||
@@ -15,20 +15,11 @@ func main() {
|
||||
s := CounterState{Count: 0}
|
||||
|
||||
step := c.Signal(1)
|
||||
greeting := c.Signal("Hello...")
|
||||
|
||||
increment := c.Action(func() {
|
||||
s.Count += step.Int()
|
||||
c.Sync()
|
||||
})
|
||||
greetBob := c.Action(func() {
|
||||
greeting.SetValue("Hello Bob!")
|
||||
c.SyncSignals()
|
||||
})
|
||||
greetAlice := c.Action(func() {
|
||||
greeting.SetValue("Hello Alice!")
|
||||
c.SyncSignals()
|
||||
})
|
||||
|
||||
c.View(func() h.H {
|
||||
return h.Div(
|
||||
@@ -39,10 +30,6 @@ func main() {
|
||||
h.Input(h.Type("number"), step.Bind()),
|
||||
),
|
||||
h.Button(h.Text("Increment"), increment.OnClick()),
|
||||
|
||||
h.P(h.Span(h.Text("Greeting: ")), h.Span(greeting.Text())),
|
||||
h.Button(h.Text("Greet Alice"), greetBob.OnClick()),
|
||||
h.Button(h.Text("Greet Alice"), greetAlice.OnClick()),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
34
internal/examples/greeter/main.go
Normal file
34
internal/examples/greeter/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/go-via/via"
|
||||
"github.com/go-via/via/h"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := via.New()
|
||||
|
||||
v.Page("/", func(c *via.Context) {
|
||||
|
||||
greeting := c.Signal("Hello...")
|
||||
|
||||
greetBob := c.Action(func() {
|
||||
greeting.SetValue("Hello Bob!")
|
||||
c.SyncSignals()
|
||||
})
|
||||
greetAlice := c.Action(func() {
|
||||
greeting.SetValue("Hello Alice!")
|
||||
c.SyncSignals()
|
||||
})
|
||||
|
||||
c.View(func() h.H {
|
||||
return h.Div(
|
||||
h.P(h.Span(h.Text("Greeting: ")), h.Span(greeting.Text())),
|
||||
h.Button(h.Text("Greet Bob"), greetBob.OnClick()),
|
||||
h.Button(h.Text("Greet Alice"), greetAlice.OnClick()),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
v.Start(":3000")
|
||||
}
|
||||
Reference in New Issue
Block a user