Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created December 12, 2017 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roman01la/c2f2a3fcf24f00f8b9abe7754a8c166f to your computer and use it in GitHub Desktop.
Save roman01la/c2f2a3fcf24f00f8b9abe7754a8c166f to your computer and use it in GitHub Desktop.
(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