partial-reload.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package e00_basics
  2. // @snippet_begin(PartialReloadSample)
  3. import (
  4. "fmt"
  5. "time"
  6. "github.com/qor5/web"
  7. . "github.com/theplant/htmlgo"
  8. )
  9. func PartialReloadPage(ctx *web.EventContext) (pr web.PageResponse, err error) {
  10. reloadCount = 0
  11. ctx.Injector.HeadHTML(`
  12. <style>
  13. .rp {
  14. float: left;
  15. width: 200px;
  16. height: 200px;
  17. margin-right: 20px;
  18. background-color: orange;
  19. }
  20. </style>
  21. `,
  22. )
  23. pr.Body = Div(
  24. H1("Portal Reload Automatically"),
  25. web.Scope(
  26. web.Portal().Loader(web.POST().EventFunc("autoReload")).AutoReloadInterval("locals.interval"),
  27. Button("stop").Attr("@click", "locals.interval = 0"),
  28. ).Init(`{interval: 2000}`).VSlot("{ locals }"),
  29. H1("Load Data Only"),
  30. web.Scope(
  31. Ul(
  32. Li(
  33. Text("{{item}}"),
  34. ).Attr("v-for", "item in locals.items"),
  35. ),
  36. Button("Fetch Data").Attr("@click", web.GET().EventFunc("loadData").ThenScript(`locals.items = r.data`).Go()),
  37. ).VSlot("{ locals }").Init("{ items: []}"),
  38. H1("Partial Load and Reload"),
  39. Div(
  40. H2("Product 1"),
  41. ).Style("height: 200px; background-color: grey;"),
  42. H2("Related Products"),
  43. web.Portal().Name("related_products").Loader(web.POST().EventFunc("related").Query("productCode", "AH123")),
  44. A().Href("javascript:;").Text("Reload Related Products").
  45. Attr("@click", web.POST().EventFunc("reload3").Go()),
  46. )
  47. return
  48. }
  49. func related(ctx *web.EventContext) (er web.EventResponse, err error) {
  50. code := ctx.R.FormValue("productCode")
  51. er.Body = Div(
  52. Div(
  53. H3("Product A (related products of "+code+")"),
  54. Div().Text(time.Now().Format(time.RFC3339Nano)),
  55. ).Class("rp"),
  56. Div(
  57. H3("Product B"),
  58. Div().Text(time.Now().Format(time.RFC3339Nano)),
  59. ).Class("rp"),
  60. Div(
  61. H3("Product C"),
  62. Div().Text(time.Now().Format(time.RFC3339Nano)),
  63. ).Class("rp"),
  64. )
  65. return
  66. }
  67. func reload3(ctx *web.EventContext) (er web.EventResponse, err error) {
  68. er.ReloadPortals = []string{"related_products"}
  69. return
  70. }
  71. var reloadCount = 1
  72. func autoReload(ctx *web.EventContext) (er web.EventResponse, err error) {
  73. er.Body = Span(time.Now().String())
  74. reloadCount++
  75. if reloadCount > 5 {
  76. er.VarsScript = `vars.interval = 0;`
  77. }
  78. return
  79. }
  80. func loadData(ctx *web.EventContext) (er web.EventResponse, err error) {
  81. var r []string
  82. for i := 0; i < 10; i++ {
  83. r = append(r, fmt.Sprintf("%d-%d", i, time.Now().Nanosecond()))
  84. }
  85. er.Data = r
  86. return
  87. }
  88. var PartialReloadPagePB = web.Page(PartialReloadPage).
  89. EventFunc("related", related).
  90. EventFunc("reload3", reload3).
  91. EventFunc("autoReload", autoReload).
  92. EventFunc("loadData", loadData)
  93. const PartialReloadPagePath = "/samples/partial_reload"
  94. // @snippet_end