-
-
Save Deraen/426285564376266c675b 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 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