Skip to content

Instantly share code, notes, and snippets.

@ampersanda
Forked from roman01la/core.cljs
Created January 31, 2018 01:56
Show Gist options
  • Save ampersanda/0b64b92fa791d4b89a02b5a63035f24c to your computer and use it in GitHub Desktop.
Save ampersanda/0b64b92fa791d4b89a02b5a63035f24c 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