mirror of
https://github.com/metosin/reitit.git
synced 2025-12-16 16:01:11 +00:00
parent
7f7d6e2256
commit
03009f5ca4
5 changed files with 79 additions and 8 deletions
|
|
@ -12,6 +12,14 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino
|
|||
|
||||
[breakver]: https://github.com/ptaoussanis/encore/blob/master/BREAK-VERSIONING.md
|
||||
|
||||
## Unreleased
|
||||
|
||||
### `reitit-frontend`
|
||||
|
||||
* `reitit.frontend.easy` state is setup before user `on-navigate` callback
|
||||
is called the first time, so that `rfe/push-state` and such can be called
|
||||
([#315](https://github.com/metosin/reitit/issues/315))
|
||||
|
||||
## 0.4.2 (2020-01-17)
|
||||
|
||||
### `reitit`
|
||||
|
|
|
|||
|
|
@ -21,11 +21,23 @@
|
|||
- on-navigate Function to be called when route changes. Takes two parameters, ´match´ and ´history´ object.
|
||||
|
||||
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 current route.
|
||||
|
||||
Options (Html5History):
|
||||
- :ignore-anchor-click? Function (router, event, anchor element, uri) which will be called to
|
||||
check if the anchor click event should be ignored.
|
||||
To extend built-in check, you can call `reitit.frontend.history/ignore-anchor-click?`
|
||||
function, which will ignore clicks if the href matches route tree."
|
||||
[router on-navigate opts]
|
||||
(swap! history (fn [old-history]
|
||||
(rfh/stop! old-history)
|
||||
(rfh/start! router on-navigate opts))))
|
||||
;; Stop and set to nil.
|
||||
(swap! history rfh/stop!)
|
||||
; ;; Store the reference to History object in navigate callback, before calling user
|
||||
; ;; callback, so that user function can call rfe functions.
|
||||
(rfh/start! router (fn rfe-on-navigate [m this]
|
||||
(when (nil? @history)
|
||||
(reset! history this))
|
||||
(on-navigate m this))
|
||||
opts))
|
||||
|
||||
(defn href
|
||||
([k]
|
||||
|
|
|
|||
|
|
@ -33,7 +33,8 @@
|
|||
:hashchange-listener (gevents/listen js/window goog.events.EventType.HASHCHANGE handler false))))
|
||||
(-stop [this]
|
||||
(gevents/unlistenByKey popstate-listener)
|
||||
(gevents/unlistenByKey hashchange-listener))
|
||||
(gevents/unlistenByKey hashchange-listener)
|
||||
nil)
|
||||
(-on-navigate [this path]
|
||||
(reset! last-fragment path)
|
||||
(on-navigate (rf/match-by-path router path) this))
|
||||
|
|
@ -123,7 +124,8 @@
|
|||
(on-navigate (rf/match-by-path router path) this))
|
||||
(-stop [this]
|
||||
(gevents/unlistenByKey listen-key)
|
||||
(gevents/unlistenByKey click-listen-key))
|
||||
(gevents/unlistenByKey click-listen-key)
|
||||
nil)
|
||||
(-get-path [this]
|
||||
(str (.. js/window -location -pathname)
|
||||
(.. js/window -location -search)))
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@
|
|||
reitit.exception-test
|
||||
reitit.frontend.core-test
|
||||
reitit.frontend.history-test
|
||||
reitit.frontend.controllers-test))
|
||||
reitit.frontend.controllers-test
|
||||
reitit.frontend.easy-test))
|
||||
|
||||
(enable-console-print!)
|
||||
|
||||
|
|
@ -22,4 +23,5 @@
|
|||
'reitit.exception-test
|
||||
'reitit.frontend.core-test
|
||||
'reitit.frontend.history-test
|
||||
'reitit.frontend.controllers-test)
|
||||
'reitit.frontend.controllers-test
|
||||
'reitit.frontend.easy-test)
|
||||
|
|
|
|||
47
test/cljs/reitit/frontend/easy_test.cljs
Normal file
47
test/cljs/reitit/frontend/easy_test.cljs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
(ns reitit.frontend.easy-test
|
||||
(:require [clojure.test :refer [deftest testing is are async]]
|
||||
[reitit.core :as r]
|
||||
[reitit.frontend.easy :as rfe]
|
||||
[reitit.frontend.history :as rfh]
|
||||
[goog.events :as gevents]))
|
||||
|
||||
(def browser (exists? js/window))
|
||||
|
||||
(def router (r/router ["/"
|
||||
["" ::frontpage]
|
||||
["foo" ::foo]
|
||||
["bar/:id" ::bar]]))
|
||||
|
||||
(deftest easy-history-routing-test
|
||||
(when browser
|
||||
(gevents/removeAll js/window goog.events.EventType.POPSTATE)
|
||||
(gevents/removeAll js/window goog.events.EventType.HASHCHANGE)
|
||||
|
||||
(async done
|
||||
(let [n (atom 0)]
|
||||
;; This also validates that rfe/history is set during initial on-navigate call
|
||||
(rfe/start! router
|
||||
(fn on-navigate [match history]
|
||||
(let [url (rfh/-get-path history)]
|
||||
(case (swap! n inc)
|
||||
1 (do (is (= "/" url)
|
||||
"start at root")
|
||||
(rfe/push-state ::foo))
|
||||
2 (do (is (= "/foo" url)
|
||||
"push-state")
|
||||
(.back js/window.history))
|
||||
3 (do (is (= "/" url)
|
||||
"go back")
|
||||
(rfe/push-state ::bar {:id 1}))
|
||||
4 (do (is (= "/bar/1" url)
|
||||
"push-state 2")
|
||||
(rfe/replace-state ::bar {:id 2}))
|
||||
5 (do (is (= "/bar/2" url)
|
||||
"replace-state")
|
||||
(.back js/window.history))
|
||||
6 (do (is (= "/" url)
|
||||
"go back after replace state")
|
||||
(rfh/stop! @rfe/history)
|
||||
(done))
|
||||
(is false "extra event"))))
|
||||
{:use-fragment true})))))
|
||||
Loading…
Reference in a new issue