mirror of
https://github.com/metosin/reitit.git
synced 2025-12-23 10:41:10 +00:00
Merge pull request #130 from metosin/frontend-routing-4
Frontend routing 4
This commit is contained in:
commit
78decdb23e
2 changed files with 13 additions and 11 deletions
|
|
@ -15,15 +15,15 @@
|
||||||
remove listeners using stop! call before calling start! again.
|
remove listeners using stop! call before calling start! again.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
- router The Reitit routing tree.
|
- router The Reitit router.
|
||||||
- on-navigate Function to be called when route changes. Takes two parameters, ´token´ and ´history´ object.
|
- on-navigate Function to be called when route changes. Takes two parameters, ´match´ and ´history´ object.
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
- :use-fragment (default true) If true, onhashchange and location hash are used to store the token."
|
- :use-fragment (default true) If true, onhashchange and location hash are used to store the token."
|
||||||
[routes on-navigate opts]
|
[router on-navigate opts]
|
||||||
(swap! history (fn [old-history]
|
(swap! history (fn [old-history]
|
||||||
(rfh/stop! old-history)
|
(rfh/stop! old-history)
|
||||||
(rfh/start! routes on-navigate opts))))
|
(rfh/start! router on-navigate opts))))
|
||||||
|
|
||||||
(defn href
|
(defn href
|
||||||
([k]
|
([k]
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
;; This version listens for both pop-state and hash-change for
|
;; This version listens for both pop-state and hash-change for
|
||||||
;; compatibility for old browsers not supporting History API.
|
;; compatibility for old browsers not supporting History API.
|
||||||
(defrecord FragmentHistory [on-navigate router listen-key last-fragment]
|
(defrecord FragmentHistory [on-navigate router popstate-listener hashchange-listener last-fragment]
|
||||||
History
|
History
|
||||||
(-init [this]
|
(-init [this]
|
||||||
;; Link clicks and e.g. back button trigger both events, if fragment is same as previous ignore second event.
|
;; Link clicks and e.g. back button trigger both events, if fragment is same as previous ignore second event.
|
||||||
|
|
@ -30,12 +30,11 @@
|
||||||
(-on-navigate this path))))]
|
(-on-navigate this path))))]
|
||||||
(-on-navigate this (-get-path this))
|
(-on-navigate this (-get-path this))
|
||||||
(assoc this
|
(assoc this
|
||||||
:listen-key (gevents/listen js/window
|
:popstate-listener (gevents/listen js/window goog.events.EventType.POPSTATE handler false)
|
||||||
#js [goog.events.EventType.POPSTATE goog.events.EventType.HASHCHANGE]
|
:hashchange-listener (gevents/listen js/window goog.events.EventType.HASHCHANGE handler false))))
|
||||||
handler
|
|
||||||
false))))
|
|
||||||
(-stop [this]
|
(-stop [this]
|
||||||
(gevents/unlistenByKey listen-key))
|
(gevents/unlistenByKey popstate-listener)
|
||||||
|
(gevents/unlistenByKey hashchange-listener))
|
||||||
(-on-navigate [this path]
|
(-on-navigate [this path]
|
||||||
(reset! last-fragment path)
|
(reset! last-fragment path)
|
||||||
(on-navigate (rf/match-by-path router path) this))
|
(on-navigate (rf/match-by-path router path) this))
|
||||||
|
|
@ -102,11 +101,14 @@
|
||||||
|
|
||||||
(defn start!
|
(defn start!
|
||||||
"This registers event listeners on HTML5 history and hashchange events.
|
"This registers event listeners on HTML5 history and hashchange events.
|
||||||
|
|
||||||
|
Returns History object.
|
||||||
|
|
||||||
When using with development workflow like Figwheel, rememeber to
|
When using with development workflow like Figwheel, rememeber to
|
||||||
remove listeners using stop! call before calling start! again.
|
remove listeners using stop! call before calling start! again.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
- router The Reitit routing tree.
|
- router The Reitit router.
|
||||||
- on-navigate Function to be called when route changes. Takes two parameters, ´match´ and ´history´ object.
|
- on-navigate Function to be called when route changes. Takes two parameters, ´match´ and ´history´ object.
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue