Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created August 7, 2014 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattdeboard/e2e473558572a202d9e6 to your computer and use it in GitHub Desktop.
Save mattdeboard/e2e473558572a202d9e6 to your computer and use it in GitHub Desktop.
(ns emp.core
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [goog.events :as events]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[secretary.core
:as secretary
:include-macros true
:refer [defroute]]
[cljs.core.async :refer [put! chan <!]])
(:import [goog History]
[goog.history EventType]))
(enable-console-print!)
(def history (History.))
(secretary/set-config! :prefix "#")
(def app-state
(atom {:showing :all
:content [{:url "content/1" :title "Content One"}
{:url "content/2" :title "Content Two"}]
:text "Hello world!"
:random (rand)}))
;; Routing
(defroute "/" [] (swap! app-state assoc :showing :all))
(defroute "/hello/:name" [name]
(swap! app-state assoc :text (str "Hello " name "!")))
(defroute "/content/:content-id" [content-id]
(swap! app-state assoc :showing (keyword content-id)))
(defn on-navigate [event]
(secretary/dispatch! (.-token event)))
(events/listen history EventType.NAVIGATE on-navigate)
(.setEnabled history true)
;; Multimethod
(defmulti content-view (fn [app owner]
(js/console.log app)
(get app :showing)))
(defmethod content-view :all
[{:keys [content]} owner]
(reify
om/IRenderState
(render-state [_ {:keys [select]}]
(let [{:keys [url title]} content]
(dom/li #js {:className "content-link" :id title}
(dom/a #js {:href (str "#/" url)
:onClick (fn [e] (put! select url))}
title))))))
;; (defmethod content-view :default
;; [{:keys [showing]} owner]
;; (reify
;; om/IRenderState
;; (render-state [_ state]
;; (dom/p nil (str "This is content #" showing)))))
;; "Views"
(defn dashboard-item-view [{:keys [url title]} owner]
(reify
om/IRenderState
(render-state [_ {:keys [select]}]
(dom/li #js {:className "content-link" :id title}
(dom/a #js {:href (str "#/" url)
:onClick (fn [e] (put! select url))}
title)))))
(defn reader-app [app owner]
(reify
om/IInitState
(init-state [_] {:select (chan)})
om/IRenderState
(render-state [_ {:keys [select]}]
(dom/div #js {:id "dashboard"}
(dom/h2 nil (:text app))
(apply dom/ul #js {:id "content-view"}
(om/build-all
;;dashboard-item-view
content-view {:showing (get app :showing)
:content (get app :content)}
;;(:content app)
{:init-state {:select select}}))))))
(om/root reader-app
app-state
{:target (. js/document (getElementById "app"))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment