Skip to content

Instantly share code, notes, and snippets.

@twillis
Created November 28, 2015 14:16
Show Gist options
  • Save twillis/d89e22c3318956ee9010 to your computer and use it in GitHub Desktop.
Save twillis/d89e22c3318956ee9010 to your computer and use it in GitHub Desktop.
studying the om.next framework just trying to keep it all in my head.
(ns om-next-tutorial.core
(:require [goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]
[cljs.pprint :as pp]))
(enable-console-print!)
;; used for initial state in reconciler
(def init-state {:app/title "Hello"})
;; read method dispatches on key
(defmulti read om/dispatch)
;; default read retrieves whatever key is from parser
(defmethod read :default [{:keys [state] :as env} key params]
{:value (get-in @state [key])})
;; react component
(defui AppRoot
;; query protocol asks for keys from parser
static om/IQuery
(query [this]
'[:app/title])
;; react component protocol
Object
;; render method
(render [this]
(let [{:keys [:app/title]} (om/props this) ;; query results
;; come in as
;; props of
;; component
]
(dom/div nil (dom/h1 nil title)) ;; render dom stuff as usual
)))
;; handles read/writes to state
(def parser (om/parser {:read read}))
;; compose parser and initial state
;; this is basically your app state atom
(def reconciler (om/reconciler {:parser parser
:state init-state}))
;; add to the dom
(om/add-root! reconciler AppRoot (gdom/getElement "app"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment