Browse Source

Added functionality to allow for back/forward button interactions.

Wirecog 6 years ago
parent
commit
305db2d29e
1 changed files with 14 additions and 0 deletions
  1. 14
    0
      router.go

+ 14
- 0
router.go View File

19
 }
19
 }
20
 
20
 
21
 func NewRouter() *Router {
21
 func NewRouter() *Router {
22
+
23
+	initializeHistoryInteractions()
24
+
22
 	return &Router{
25
 	return &Router{
23
 		routes: []*Route{},
26
 		routes: []*Route{},
24
 	}
27
 	}
43
 	links := document.QuerySelectorAll(querySelector)
46
 	links := document.QuerySelectorAll(querySelector)
44
 
47
 
45
 	for _, link := range links {
48
 	for _, link := range links {
49
+
46
 		href := link.GetAttribute("href")
50
 		href := link.GetAttribute("href")
47
 		switch {
51
 		switch {
48
 
52
 
55
 			r.listener = link.AddEventListener("click", false, r.linkHandler)
59
 			r.listener = link.AddEventListener("click", false, r.linkHandler)
56
 		}
60
 		}
57
 	}
61
 	}
62
+
58
 }
63
 }
59
 
64
 
60
 func (r *Router) linkHandler(event dom.Event) {
65
 func (r *Router) linkHandler(event dom.Event) {
66
+
61
 	uri := event.CurrentTarget().GetAttribute("href")
67
 	uri := event.CurrentTarget().GetAttribute("href")
62
 	path := strings.Split(uri, "?")[0]
68
 	path := strings.Split(uri, "?")[0]
63
 	//	leastParams := -1
69
 	//	leastParams := -1
98
 		go matchedRoute.handler.ServeRoute(ctx)
104
 		go matchedRoute.handler.ServeRoute(ctx)
99
 	}
105
 	}
100
 }
106
 }
107
+
108
+func initializeHistoryInteractions() {
109
+	// Handler for back/forward button interactions
110
+	dom.GetWindow().AddEventListener("popstate", false, func(event dom.Event) {
111
+		var location = js.Global.Get("location")
112
+		js.Global.Set("location", location)
113
+	})
114
+}