reload-with-a-flash.go 971 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package e00_basics
  2. // @snippet_begin(ReloadWithFlashSample)
  3. import (
  4. "fmt"
  5. "time"
  6. "github.com/qor5/web"
  7. . "github.com/theplant/htmlgo"
  8. )
  9. var count int
  10. func ReloadWithFlash(ctx *web.EventContext) (pr web.PageResponse, err error) {
  11. var msg HTMLComponent
  12. if d, ok := ctx.Flash.(*Data1); ok {
  13. msg = Div().Text(d.Msg).Style("border: 5px solid orange;")
  14. } else {
  15. count = 0
  16. }
  17. pr.Body = Div(
  18. H1("Whole Page Reload With a Flash"),
  19. msg,
  20. Div().Text(time.Now().Format(time.RFC3339Nano)),
  21. Button("Do Something").
  22. Attr("@click", web.POST().EventFunc("update2").Go()),
  23. )
  24. return
  25. }
  26. type Data1 struct {
  27. Msg string
  28. }
  29. func update2(ctx *web.EventContext) (er web.EventResponse, err error) {
  30. count++
  31. ctx.Flash = &Data1{Msg: fmt.Sprintf("The page is reloaded: %d", count)}
  32. er.Reload = true
  33. return
  34. }
  35. var ReloadWithFlashPB = web.Page(ReloadWithFlash).EventFunc("update2", update2)
  36. const ReloadWithFlashPath = "/samples/reload_with_flash"
  37. // @snippet_end