Skip to content

Improve messages/bindings

Compare
Choose a tag to compare
@maxence-charriere maxence-charriere released this 04 Sep 23:16
· 428 commits to master since this release

app

  • Binding can execute a function on the UI goroutine.
  • Binding can wait for a duration before execution the next function.
  • Message can be emitted from html events.
func (h *Hello) OnMount() {
	// Binding to the nameChanged msg that wait 1 second and print logs on
	// binding an ui gouroutines.
	app.Bind("nameChanged", h).
		Wait(time.Second).
		Do(func() {
			log.Debug("log from binding goroutine")
		}).
		DoOnUI(func() {
			log.Debug("log from UI goroutine")
		}).
		Do(func() {
			log.Debug("another log from binding goroutine")
		})

	// Binding that is called from an html event and modify the Name field.
	app.Bind("nameChanged", h).
		DoOnUI(func(s, e js.Value) {
			h.Name = s.Get("value").String()
			app.Render(h)
		})
}

func (h *Hello) Render() string {
	// Input emit a "nameChanged" message when the onchange event occurs.
	return `
<div>
	<!-- ... -->
	<input 
		value="{{.Name}}" 
		placeholder="What is your name?" 
		onchange={{emit nameChanged}} 
		autofocus>
	<!-- ... -->
</div>
	`
}