Skip to content

Instantly share code, notes, and snippets.

@nnarhinen
Created October 1, 2013 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnarhinen/6786086 to your computer and use it in GitHub Desktop.
Save nnarhinen/6786086 to your computer and use it in GitHub Desktop.
Some experimental code for navigation handling with core.async
(ns myapp.main
(:require [jayq.core :refer [on $] :as j]
[cljs.core.async :as async
:refer [<! chan put!]]
[crate.core :as crate])
(:require-macros [cljs.core.async.macros :as m :refer [go alt!]]
[crate.def-macros :refer [defpartial]]))
(def $container ($ :.main-container))
(defn navigation-key []
(let [location (.-location js/window)
h (subs (.-hash location) 1)]
(keyword h)))
(def hashchange-chan (let [rc (chan)]
(on ($ js/window) :hashchange {}
(fn [e]
(let [h (navigation-key)]
(put! rc [:navigate h]))))
rc))
(defpartial customers-layout []
[:h1 "Customers"])
(defpartial invoices-layout []
[:h1 "Invoices"])
(def views {
:customers customers-layout,
:invoices invoices-layout})
(def start-state {:navigation (navigation-key)})
(go
(loop [state start-state]
(j/empty $container)
(j/append $container (((:navigation state) views)))
(let [[msg-name msg-data] (<! hashchange-chan)]
(recur (assoc state :navigation msg-data)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment