Skip to content

Instantly share code, notes, and snippets.

@jobez
Last active August 29, 2015 13:56
Show Gist options
  • Save jobez/8984511 to your computer and use it in GitHub Desktop.
Save jobez/8984511 to your computer and use it in GitHub Desktop.
(ns select.core
(:require [om.core :as om :include-macros true]
[om.dom :as dom :include-macros true])
(:import [goog.ui IdGenerator] ;; unique id goodness
))
(defn guid []
(.getNextUniqueId (.getInstance IdGenerator)))
(enable-console-print!)
(def app-state (atom {:things (->> (range 3)
rest
(map (partial str "thing-"))
(map (partial hash-map :name))
vec)
:more-things []}))
(extend-type js/HTMLCollection
ISeqable
(-seq [collection]
(-> js/Array
.-prototype
.-slice
(.call collection))))
(defn thing
[thing]
(om/component
(dom/option nil (:name thing))))
(defn select-handle [thing]
(om/transact! thing :selected not))
(defn thing-unique [{:keys [selected name] :as thing]
(print thing)
(om/component
(dom/option #js {:value (if selected "a" "z") :onClick #(select-handle thing)} name)))
(defn handle-add
[app owner]
(let [things (->> (om/get-node owner "listing")
.-children
(filter #(.-selected %1))
(map #(.-text %1)))
get-thing (fn [thing-name]
(->> @app-state
:things
(filter (fn [{name :name}]
(= thing-name name)))
first
))
thing (->> things
(map get-thing)
(into {}))
thing-unique (assoc thing :id (guid) :selected false)
]
(om/transact! app
:more-things
conj thing-unique)))
(defn listing
[app owner]
(om/component
(dom/div nil
(apply dom/select #js {:ref "listing"}
(om/build-all thing (:things app)))
(dom/button #js {:onClick #(handle-add app owner)}
"add"))))
(defn more-list
[things]
(om/component
(apply dom/select #js {:multiple true :value #js ["a" "b" "c"]}
(om/build-all thing things))))
(om/root
app-state
(fn [app owner]
(dom/div nil
(om/build listing app)
(om/build more-list (:more-things app))))
(. js/document (getElementById "app")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment