Skip to content

Instantly share code, notes, and snippets.

@sander
Last active November 29, 2015 19:28
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 sander/97c50192c24d686a1e3d to your computer and use it in GitHub Desktop.
Save sander/97c50192c24d686a1e3d to your computer and use it in GitHub Desktop.
(ns om-tutorial.core
(:require [goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]))
(enable-console-print!)
(def init-data
{:current-user {:email "bob.smith@gmail.com"}
:items [{:id 0 :title "Foo"}
{:id 1 :title "Bar"}
{:id 2 :title "Baz"}]})
(defmulti read om/dispatch)
(defmethod read :items
[{:keys [query state]} k _]
(let [st @state]
{:value (om/db->tree query (get st k) st)}))
(defn mutate
[{:keys [state] :as env} key params]
(if (= 'change-email key)
{:value {:keys [:items]}
:action #(swap! state assoc-in [:current-user :email] "alternative@email.example")}
{:value :not-found}))
(defui Item
static om/Ident
(ident [_ {:keys [id]}]
[:item/by-id id])
static om/IQuery
(query [_]
'[:id :title [:current-user _]])
Object
(render [this]
(let [{:keys [title current-user]} (om/props this)]
(dom/li nil
(dom/div nil title)
(dom/div nil (:email current-user) " "
(dom/button #js {:onClick #(om/transact! this '[(change-email)])}
"Change"))))))
(def item (om/factory Item))
(defui SomeList
static om/IQuery
(query [_]
[{:items (om/get-query Item)}])
Object
(render [this]
(dom/div
(dom/h2 nil "A List!")
(dom/ul nil
(map item (-> this om/props :items))))))
(def reconciler
(om/reconciler
{:state init-data
:parser (om/parser {:read read
:mutate mutate})}))
(om/add-root! reconciler SomeList (gdom/getElement "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment