Last active
November 29, 2015 19:28
-
-
Save sander/97c50192c24d686a1e3d to your computer and use it in GitHub Desktop.
Mutation added to https://github.com/omcljs/om/wiki/Thinking-With-Links%21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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