Skip to content

Instantly share code, notes, and snippets.

@colinkahn
Created January 9, 2014 17:14
Show Gist options
  • Save colinkahn/8338003 to your computer and use it in GitHub Desktop.
Save colinkahn/8338003 to your computer and use it in GitHub Desktop.
(ns omni.main
(:require-macros [cljs.core.async.macros :refer [go alt!]])
(:require [cljs.core.async :refer [>! put! chan]]
[om.core :as om :include-macros true]
[om.dom :as dom :include-macros true])
(:import [goog.ui IdGenerator]))
(defn guid []
(.getNextUniqueId (.getInstance IdGenerator)))
(def app-state (atom {:projects [{:name "starter" :id (guid)}]}))
(defn add-project [app]
(let [project-name (.-value (.getElementById js/document "project-name"))]
(om/transact! app [:projects] conj {:name project-name :id (guid)})))
(defn remove-project [app proj]
(let [id (om/read proj :id)]
(om/transact! app [:projects]
(fn [projs]
(into [] (remove #(= (:id %) id) projs))))))
(defn handle-event [app [type proj :as e]]
(case type
:add (add-project app)
:remove (remove-project app proj)))
(defn project [data owner {:keys [app comm]}]
(om/component
(dom/div nil (:name data)
(dom/button #js {:onClick #(put! comm [:remove data])} "Delete"))))
(defn projects [app owner {:keys [comm]}]
(om/component
(dom/div nil (om/build-all project (:projects app) {:opts {:comm comm} :key :id})
(dom/input #js {:id "project-name"})
(dom/button #js {:onClick #(put! comm [:add])} "Add Project"))))
(defn projects-app [app owner]
(reify
om/IWillMount
(will-mount [_]
(let [comm (chan)]
(om/set-state! owner [:comm] comm)
(go (while true
(handle-event app (<! comm))))))
om/IRender
(render [_]
(let [comm (om/get-state owner [:comm])]
(dom/div nil
(om/build projects app {:opts {:comm comm}}))))))
(om/root app-state projects-app js/document.body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment