Skip to content

Instantly share code, notes, and snippets.

@marianoguerra
Last active October 13, 2015 15:25
Show Gist options
  • Save marianoguerra/a92abce70ca6371faeff to your computer and use it in GitHub Desktop.
Save marianoguerra/a92abce70ca6371faeff to your computer and use it in GitHub Desktop.
First attempt at using specter with om.next, the mutate function is quite hacky since I'm sending all the info in the params and ignoring te query
(ns iorioui.core
(:require
[goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]
[com.rpl.specter :as s]
[cljs-http.client :as http]
[cljs.core.async :refer [<! >! put! chan]])
(:require-macros
[cljs.core.async.macros :refer [go]]))
(enable-console-print!)
(def app-state (atom {:count 0}))
(defn read [{:keys [state] :as env} query params]
(let [st @state]
(if-let [[value] (s/select query st)]
{:value value}
{:value :not-found})))
(defmulti mutate om/dispatch)
(defmethod mutate 'app/increment
[{:keys [state]} _ params]
{:value [:count]
:action #(swap! state (fn [old] (s/transform :count inc old)))})
(defui Counter
static om/IQuery
(query [this]
[:count])
Object
(render [this]
(let [{:keys [count]} (om/props this)]
(dom/div nil
(dom/span nil (str "Count: " count))
(dom/button
#js {:onClick #(om/transact! this `[(app/increment)])}
"Click me!")))))
(def reconciler
(om/reconciler
{:state app-state
:parser (om/parser {:read read :mutate mutate})}))
(om/add-root! reconciler
Counter (gdom/getElement "main-app-area"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment