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,6 +19,9 @@ type Router struct {
19 19
 }
20 20
 
21 21
 func NewRouter() *Router {
22
+
23
+	initializeHistoryInteractions()
24
+
22 25
 	return &Router{
23 26
 		routes: []*Route{},
24 27
 	}
@@ -43,6 +46,7 @@ func (r *Router) RegisterLinks(querySelector string) {
43 46
 	links := document.QuerySelectorAll(querySelector)
44 47
 
45 48
 	for _, link := range links {
49
+
46 50
 		href := link.GetAttribute("href")
47 51
 		switch {
48 52
 
@@ -55,9 +59,11 @@ func (r *Router) RegisterLinks(querySelector string) {
55 59
 			r.listener = link.AddEventListener("click", false, r.linkHandler)
56 60
 		}
57 61
 	}
62
+
58 63
 }
59 64
 
60 65
 func (r *Router) linkHandler(event dom.Event) {
66
+
61 67
 	uri := event.CurrentTarget().GetAttribute("href")
62 68
 	path := strings.Split(uri, "?")[0]
63 69
 	//	leastParams := -1
@@ -98,3 +104,11 @@ func (r *Router) linkHandler(event dom.Event) {
98 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
+}