Skip to content

Instantly share code, notes, and snippets.

@Deraen
Last active July 14, 2016 17:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deraen/426285564376266c675b to your computer and use it in GitHub Desktop.
Save Deraen/426285564376266c675b to your computer and use it in GitHub Desktop.
(ns foobar.routes
(:require [domkm.silk :as silk]
[foobar.state :refer [app-state]]))
(def app-routes
(silk/routes [[:frontpage [[]]] ; "/#/"
[:new-thingie [["thingies" "new"]]] ; "/#/thingies/new"
[:thingie [["thingies" :id]]]])) ; "/#/thingies/:id"
(defn href
[page & [params]]
(try
(str "#" (silk/depart app-routes page params))
(catch js/Error _
(println "Couln't create href for" page)
nil)))
(defn go!
[page & [params]]
(set! js/window.location.hash (apply href page params)))
(defn update-route! []
(let [uri (some-> js/window.location.hash (.substr 1))
page (silk/arrive app-routes uri)]
(swap! app-state assoc )))
(defn hook-dispatch-to-on-hashchange! []
(-> js/window .-onhashchange (set! (fn [_] (update-route!))))
(update-route!))
; Example link
[:a {:href (href :thingie {:id 5})} "Thingie 5"]
; <a href="#/thingies/5">Thingie 5</a>
; After succesfully saving a new thingie, move to created items page
(go
(let [new-thingie (->> "/api/thingie" http/post <! :body)]
(go! :thingie (select-keys new-thingie [:id])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment