re-natal/resources/om-next.cljs

53 lines
1.5 KiB
Plaintext
Raw Normal View History

2015-10-31 18:48:25 +00:00
;; Need to set js/React first so that Om can load
(set! js/React (js/require "react-native/Libraries/react-native/react-native.js"))
(ns $PROJECT_NAME_HYPHENATED$.core
(:require [om.next :as om :refer-macros [defui]]))
2015-11-15 22:04:17 +00:00
;; Reset js/React back as the form above loads in a different React
2015-10-31 18:48:25 +00:00
(set! js/React (js/require "react-native/Libraries/react-native/react-native.js"))
2015-11-15 22:04:17 +00:00
;; Set up some methods to help create React Native elements
2015-10-31 18:48:25 +00:00
(defn view [opts & children]
(apply js/React.createElement js/React.View (clj->js opts) children))
(defn text [opts & children]
(apply js/React.createElement js/React.Text (clj->js opts) children))
;; Set up our Om UI
(defonce app-state (atom {:app/msg "Welcome to $PROJECT_NAME$"}))
(defui WidgetComponent
static om/IQuery
(query [this]
'[:app/msg])
Object
(render [this]
(let [{:keys [app/msg]} (om/props this)]
(view {:style {:flexDirection "column" :margin 40}}
2015-11-14 23:16:30 +00:00
(text {:style {:fontSize 50 :fontWeight "100"}} msg)))))
2015-10-31 18:48:25 +00:00
;; om.next parser
(defmulti read om/dispatch)
(defmethod read :default
[{:keys [state]} k _]
(let [st @state]
(if-let [[_ v] (find st k)]
{:value v}
{:value :not-found})))
(def reconciler
(om/reconciler
{:state app-state
:parser (om/parser {:read read})
:root-render #(.render js/React %1 %2)
:root-unmount #(.unmountComponentAtNode js/React %)}))
(om/add-root! reconciler WidgetComponent 1)
(defn ^:export init []
((fn render []
(.requestAnimationFrame js/window render))))