page-func-and-event-func.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package advanced_functions
  2. import (
  3. "github.com/qor5/docs/docsrc/examples/e00_basics"
  4. "github.com/qor5/docs/docsrc/generated"
  5. "github.com/qor5/docs/docsrc/utils"
  6. . "github.com/theplant/docgo"
  7. "github.com/theplant/docgo/ch"
  8. )
  9. var PageFuncAndEventFunc = Doc(
  10. Markdown(`
  11. ~PageFunc~ is used to build a web page, ~EventFunc~ is called when user interact with the page, For example button or link clicks.
  12. `),
  13. ch.Code(generated.PageFuncAndEventFuncDefinition).Language("go"),
  14. Markdown(`~web.Page(...)~ converts multiple ~EventFunc~s along with one ~PageFunc~ to a ~http.Handler~,
  15. event func needs a name to be used by ~web.POST().EventFunc(name).Go()~ to attach to an html element that post http request to call the ~EventFunc~ when vue event like ~@click~ happens`),
  16. Markdown("Here is a hello world with more interactions. User click the button will reload the page with latest time"),
  17. ch.Code(generated.HelloWorldReloadSample).Language("go"),
  18. utils.Demo("Page Func and Event Func", e00_basics.HelloWorldReloadPath, "e00_basics/hello-world-reload.go"),
  19. Markdown("Note that you have to mount the `web.Page(...)` instance to http.ServeMux with a path to be able to access the ~PageFunc~ in your browser, when mounting you can also wrap the ~PageFunc~ with middleware, which is ~func(in PageFunc) (out PageFunc)~ a func that take a page func and do some wrapping and return a new page func"),
  20. ch.Code(generated.HelloWorldReloadMuxSample1).Language("go"),
  21. Markdown("~wb.Page(...)~ convert any `PageFunc` into `http.Handler`, outside you can wrap any middleware that can use on Go standard `http.Handler`."),
  22. Markdown(`In case you don't know what is a http.Handler middleware,
  23. It's a function that takes http.Handler as input, might also with other parameters,
  24. And also return a new http.Handler,
  25. [gziphandler](https://github.com/nytimes/gziphandler) is an example.`),
  26. Markdown(`But What the heck is ~demoLayout~ there?
  27. Well it's a ~PageFunc~ middleware. That takes an ~PageFunc~ as input,
  28. wrap it's ~PageResponse~ with layout html and return a new ~PageFunc~.
  29. If you follow the code to write your own ~PageFunc~,
  30. The button click might not work without this.
  31. Since there is no layout to import needed javascript to make this work.
  32. continue to next page to checkout how to add necessary javascript, css etc to make the demo work.`),
  33. ).Title("Page Func and Event Func").
  34. Slug("basics/page-func-and-event-func")