Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Om next issue
(ns om-issue.core
(:require [goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]))
(def state {:numbers/selected nil
:numbers/list [{:id 1 :name "one"}
{:id 5 :name "five"}
{:id 8 :name "eight"}
{:id 9 :name "nine"}]})
(defmulti read om/dispatch)
(defmethod read :default
[{:keys [state query]} key _]
(let [st @state]
{:value (om/db->tree query (get st key) st)}))
(defmulti mutate om/dispatch)
(defmethod mutate 'numbers/select
[{:keys [state ref]} _ _]
{:action #(swap! state assoc :numbers/selected ref)})
(def parser (om/parser {:read read :mutate mutate}))
(def reconciler (om/reconciler {:state state :parser parser}))
(defui Number
om/Ident
(ident [_ {:keys [id]}]
[:number/by-id id])
om/IQuery
(query [_]
[:id :name])
Object
(render [this]
(let [{:keys [id name]} (om/props this)]
(dom/h3 #js {:onClick #(om/transact! this
`[(numbers/select) :numbers/selected [:number/by-id ~id]])}
name))))
(def number (om/factory Number {:key-fn :id}))
(defui NumberApp
om/IQuery
(query [_]
[{:numbers/selected (om/get-query Number)}
{:numbers/list (om/get-query Number)}])
Object
(render [this]
(let [{:keys [numbers/selected numbers/list]} (om/props this)]
(dom/div nil
(dom/div nil
"Selected Number: " (:name selected))
(dom/div nil
"Number's List: "
(apply dom/span nil (map number list)))))))
(om/add-root! reconciler NumberApp (gdom/getElement "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.