Skip to content

Instantly share code, notes, and snippets.

Last active Jul 26, 2016
What would you like to do?
<!DOCTYPE html>
<meta charset="UTF-8">
<link href="css/style.css" rel="stylesheet" type="text/css">
<div id="registry"></div>
<script src="js/compiled/clojure_manager.js" type="text/javascript"></script>
(ns ^:figwheel-always clojure-om-starter.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true]
[clojure.string :as string]))
(defonce app-state
[{:type :student :first "Ben" :last "Bitdiddle" :email ""}
{:type :student :first "Alyssa" :middle-initial "P" :last "Hacker"
:email ""}
{:type :professor :first "Gerald" :middle "Jay" :last "Sussman"
:email "" :classes [:6001 :6946]}
{:type :student :first "Eva" :middle "Lu" :last "Ator" :email ""}
{:type :student :first "Louis" :last "Reasoner" :email ""}
{:type :professor :first "Hal" :last "Abelson" :email ""
:classes [:6001]}]
{:6001 "The Structure and Interpretation of Computer Programs"
:6946 "The Structure and Interpretation of Classical Mechanics"
:1806 "Linear Algebra"}}))
(defn middle-name [{:keys [middle middle-initial]}]
middle (str " " middle)
middle-initial (str " " middle-initial ".")))
(defn display-name [{:keys [first last] :as contact}]
(str last ", " first (middle-name contact)))
(defn student-view [student owner]
(render [_]
(dom/li nil (display-name student)))))
(defn professor-view [professor owner]
(render [_]
(dom/li nil
(dom/div nil (display-name professor))
(dom/label nil "Classes")
(apply dom/ul nil
(map #(dom/li nil %) (:classes professor)))))))
(defmulti entry-view (fn [person _] (:type person)))
(defmethod entry-view :student
[person owner] (student-view person owner))
(defmethod entry-view :professor
[person owner] (professor-view person owner))
(defn people [data]
(->> data
(mapv (fn [x]
(if (:classes x)
(update-in x [:classes]
(fn [cs] (mapv (:classes data) cs)))
(defn registry-view [data owner]
(render [_]
(dom/div #js {:id "registry"}
(dom/h2 nil "Registry")
(apply dom/ul nil
(om/build-all entry-view (people data)))))))
(om/root registry-view app-state
{:target (. js/document (getElementById "registry"))})
(defn on-js-reload []
;; optionally touch your app-state to force rerendering depending on
;; your application
;; (swap! app-state update-in [:__figwheel_counter] inc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment