Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
Created August 7, 2014 16:16
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/a7e20533ff59cee29004 to your computer and use it in GitHub Desktop.
Save mattdeboard/a7e20533ff59cee29004 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 [_ _] (get @app-state :showing)))
(defmethod content-view :all
[content owner]
(reify
om/IRenderState
(render-state [_ {:keys [select]}]
(let [{:keys [url title]} content]
(js/console.log 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
[content owner]
(reify
om/IRenderState
(render-state [_ state]
(js/console.log content)
(dom/p nil (str "This is content #" (get @app-state :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]}]
(let [showing (get app :showing)]
(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 (:content app)
;;(: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