Created
December 12, 2017 14:17
-
-
Save roman01la/c2f2a3fcf24f00f8b9abe7754a8c166f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns example.core | |
(:require [bidi.bidi :as bidi] | |
[goog.events :as events] | |
[clojure.string :as cstr])) | |
(defn start! [on-set-page routes] | |
(letfn [(handle-route [] | |
(let [uri (cstr/replace js/location.hash "#" "")] | |
(->> (if-not (empty? uri) uri "/") | |
(bidi/match-route routes) | |
on-set-page)))] | |
(events/listen js/window "hashchange" handle-route) | |
(handle-route) | |
handle-route)) | |
(defn stop! [handler] | |
(events/unlisten js/window "hashcange" handler)) | |
;; usage | |
(start! on-route-change routes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment