switch-pages-with-push-state.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. . "github.com/theplant/htmlgo"
  9. )
  10. var SwitchPagesWithPushState = Doc(
  11. Markdown(`Ways that page transition (between ~web.PageFunc~) in QOR5 web app:
  12. - Use a traditional link to a new page by url
  13. - Use a push state link to a new page that only change the current page body to new page body and browser url
  14. - Use a button etc to trigger post to an ~web.EventFunc~ that do some logic, then go to a new page
  15. Inside ~web.EventFunc~, two ways go to a new page:
  16. - Use [push state](https://developer.mozilla.org/en-US/docs/Web/API/History_API#Examples) to only reload the body of the new page, This won't reload javascript and css assets.
  17. - Use redirect url to reload the whole new page, This will reload target new page's javascript and css assets.
  18. This example demonstrated the above:
  19. `),
  20. ch.Code(generated.PageTransitionSample).Language("go"),
  21. utils.Demo("Switch Pages With Push State", e00_basics.Page1Path, "e00_basics/page-transition.go"),
  22. Markdown(`
  23. When running the above demo, If you check Chrome Developer Tools about Network requests,
  24. You will see that the Location link and the Button is actually doing an AJAX request to the other page.
  25. Look like this:
  26. ~~~
  27. POST /samples/page_2?__execute_event__=__reload__ HTTP/1.1
  28. ~~~
  29. The result is an JSON object with page's html inside.
  30. ~__reload__~ is another ~web.EventFunc~ that is the same as ~doAction2~,
  31. But it is default added to every ~web.PageFunc~. So that the web page can
  32. both respond to normal HTTP request from Browser, Search Engine, Or from
  33. other pages in the same web app that can do push state link.
  34. `),
  35. utils.Anchor(H2(""), "Summary"),
  36. Markdown(`
  37. - Write once with PageFunc, you get both normal html page render, and AJAX JSON page render
  38. - EventFunc is always called with AJAX request, and you can return to a different page, or rerender the current page,
  39. - EventFunc is not wrapped with layout function.
  40. - EventFunc is used to do data operations, triggered by page's html element. and it's result can be:
  41. 1. Go to a new page
  42. 2. Reload the whole current page
  43. 3. Update partial of the current page
  44. Next we will talk about how to reload the whole current page, and update partial of the current page.
  45. `),
  46. ).Title("Switch Pages with Push State").
  47. Slug("basics/switch-pages-with-push-state")