Skip to content

Instantly share code, notes, and snippets.

@alandipert
Last active May 1, 2017 16:32
Show Gist options
  • Save alandipert/523b1ba3062a7f345834 to your computer and use it in GitHub Desktop.
Save alandipert/523b1ba3062a7f345834 to your computer and use it in GitHub Desktop.
Hash fragment routing in Hoplon with core.match
(page "index.html"
(:require-macros [cljs.core.match.macros :refer [match]])
(:require [cljs.core.match]
[tailrecursion.hoplon.reload :refer [reload-all]])
(:refer-clojure :exclude [hash]))
(reload-all 1000)
(defn route [parts]
(match [parts]
[["user" id]] {:user (js/parseInt id)}
[["game" g "level" n]] {:game g, :level (js/parseInt n)}
:else {:show "Home"}))
(def hash (-> js/window .-location .-hash))
(def parts (-> hash (.split "/") next vec))
(defc place (route parts))
(defn describe [place]
(match [place]
[{:game g :level n}] (str "playing " g " on level " n)
[{:user id}] (str "viewing user " id)
:else "abiding"))
(defc= description (describe place))
(html
(head)
(body
(h1 (text "You're ~{description}"))
(h2 "Links to places")
(p "These are links to states within this application that you can share.")
(ul
(li (a :target "_blank" :href "#/game/contra/level/32" "Play Contra on Level 32"))
(li (a :target "_blank" :href "#/user/32" "See User 123")))
(h2 "Go to places without reloading")
(p "These links take you to places in the application right now.")
(ul
(li (a :href "#" :click #(reset! place {:game "Contra" :level 32}) "Play Contra on Level 32"))
(li (a :href "#" :click #(reset! place {:user 123}) "See User 123")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment