app.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // +build js
  2. package main
  3. import (
  4. "net"
  5. "net/rpc"
  6. "github.com/cryptix/exp/humbleRpcTodo/frontend/views"
  7. "github.com/gopherjs/websocket"
  8. "github.com/soroushjp/humble/router"
  9. "github.com/soroushjp/humble/view"
  10. "honnef.co/go/js/console"
  11. "honnef.co/go/js/dom"
  12. )
  13. const (
  14. appDivSelector = "#app"
  15. )
  16. var (
  17. doc = dom.GetWindow().Document()
  18. elements = struct {
  19. body dom.Element
  20. todoList dom.Element
  21. newTodo dom.Element
  22. toggleBtn dom.Element
  23. }{}
  24. appHasLoaded = false
  25. )
  26. func init() {
  27. elements.body = doc.QuerySelector(appDivSelector)
  28. }
  29. var (
  30. conn net.Conn
  31. client *rpc.Client
  32. )
  33. func main() {
  34. console.Log("Starting...")
  35. var err error
  36. conn, err = websocket.Dial("ws://localhost:3000/rpc-websocket")
  37. if err != nil {
  38. console.Error("Dial faild", err)
  39. return
  40. }
  41. client = rpc.NewClient(conn)
  42. console.Log("dialed...")
  43. //Start main app view, appView
  44. appView := &views.App{
  45. Client: client,
  46. }
  47. if err := view.ReplaceParentHTML(appView, appDivSelector); err != nil {
  48. panic(err)
  49. }
  50. r := router.New()
  51. r.HandleFunc("/", func(params map[string]string) {
  52. appView.InitChildren()
  53. if err := view.Update(appView); err != nil {
  54. panic(err)
  55. }
  56. if err := view.Update(appView.Footer); err != nil {
  57. panic(err)
  58. }
  59. appView.ApplyFilter(views.FilterAll)
  60. })
  61. r.HandleFunc("/active", func(params map[string]string) {
  62. appView.InitChildren()
  63. if err := view.Update(appView); err != nil {
  64. panic(err)
  65. }
  66. if err := view.Update(appView.Footer); err != nil {
  67. panic(err)
  68. }
  69. appView.ApplyFilter(views.FilterActive)
  70. })
  71. r.HandleFunc("/completed", func(params map[string]string) {
  72. appView.InitChildren()
  73. if err := view.Update(appView); err != nil {
  74. panic(err)
  75. }
  76. if err := view.Update(appView.Footer); err != nil {
  77. panic(err)
  78. }
  79. appView.ApplyFilter(views.FilterCompleted)
  80. })
  81. r.HandleFunc("/completed", func(params map[string]string) {
  82. console.Log("At Completed")
  83. })
  84. r.Start()
  85. }