Sfoglia il codice sorgente

minimal example for modifying map entries.

requires both an implementation of `IClonable` for entry types and a
modified version of `om` (cursors for `IClonable`s have to implement
`ITransact`).
Lucas Stadler 12 anni fa
parent
commit
7d8d892fc4
2 ha cambiato i file con 26 aggiunte e 27 eliminazioni
  1. 24 25
      clj/clarity/cljs/clarity.cljs
  2. 2 2
      clj/clarity/project.clj

+ 24 - 25
clj/clarity/cljs/clarity.cljs

@ -4,23 +4,16 @@
4 4
5 5
(enable-console-print!)
6 6
7
(extend-type string
8
  ICloneable
9
  (-clone [s] (js/String. s)))
10
7 11
; data for one node: type and optionally data
8 12
;  if data is present, fill the node with the data
9 13
;  on input, check if input conforms to data, if it
10 14
;  does change the data, if it doesn't signal the
11 15
;  error and don't change the data
12 16
13
(def example-state
14
  (atom
15
   {:type 'String
16
    :data "hello"}))
17
18
(defmulti make-typed-input
19
  (fn [{type :type} & _]
20
    (if (seq? type)
21
      (first type)
22
      type)))
23
24 17
(defn change-data
25 18
  ([ev d]
26 19
   (om/update! d assoc :data (.. ev -target -value)))
@ -37,22 +30,28 @@
37 30
      (keyword name)
38 31
      nil)))
39 32
40
(defmethod make-typed-input 'Keyword [d owner]
41
  (reify
42
    om/IRender
43
    (render [_]
44
      (dom/input #js {:type text
45
                      :value (om/read d :data)
46
                      :placeholder ":my.ns/identifier"
47
                      :pattern "^:(\\w+|\\w+(\\.\\w+)*\\/\\w+)$"
48
                      :onChange #(change-data % d valid? read-keyword)}))))
33
(defn typed-string [string owner]
34
  (om/component
35
    (dom/input #js {:type "text"
36
                    :className "field"
37
                    :value (om/value string)
38
                    :onChange #(om/update! string (fn [o n] n) (.. % -target -value))})))
49 39
50
(defmethod make-typed-input 'String [d owner]
40
(defn typed-input [data owner]
51 41
  (reify
42
    om/IWillUpdate
43
    (will-update [_ p s] (prn p s))
52 44
    om/IRender
53 45
    (render [_]
54
      (dom/input #js {:type "text"
55
                      :value (om/read d :data)
56
                      :onChange #(change-data % d)}))))
46
      (dom/div nil
47
        (dom/span nil "{")
48
        (om/build typed-string (:str data))
49
        (dom/span nil "}")))))
50
51
(def app-state
52
  (atom
53
    {:kw :hello
54
     :str "hello"
55
     :many [1 2 3]}))
57 56
58
(om/root (atom {:type 'Keyword}) make-typed-input (.getElementById js/document "typed_input"))
57
(om/root app-state typed-input (.getElementById js/document "typed_input"))

+ 2 - 2
clj/clarity/project.clj

@ -5,7 +5,7 @@
5 5
6 6
                 [com.datomic/datomic-free "0.9.4331"]
7 7
8
                 [om "0.1.5"]
8
                 [om "0.1.6"]
9 9
10 10
                 [ring "1.2.1"]
11 11
                 [compojure "1.1.6"]
@ -16,4 +16,4 @@
16 16
  :ring {:handler clarity.server/app}
17 17
  :cljsbuild {:crossovers [clarity.types]
18 18
              :crossover-path "target/crossover"
19
              :builds [{:source-paths ["cljs"]}]})
19
              :builds [{:source-paths ["om/src" "cljs"]}]})