Просмотр исходного кода

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
Родитель
Сommit
7d8d892fc4
2 измененных файлов с 26 добавлено и 27 удалено
  1. 24 25
      clj/clarity/cljs/clarity.cljs
  2. 2 2
      clj/clarity/project.clj

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

4
4
5
(enable-console-print!)
5
(enable-console-print!)
6
6
7
(extend-type string
8
  ICloneable
9
  (-clone [s] (js/String. s)))
10
7
; data for one node: type and optionally data
11
; data for one node: type and optionally data
8
;  if data is present, fill the node with the data
12
;  if data is present, fill the node with the data
9
;  on input, check if input conforms to data, if it
13
;  on input, check if input conforms to data, if it
10
;  does change the data, if it doesn't signal the
14
;  does change the data, if it doesn't signal the
11
;  error and don't change the data
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
(defn change-data
17
(defn change-data
25
  ([ev d]
18
  ([ev d]
26
   (om/update! d assoc :data (.. ev -target -value)))
19
   (om/update! d assoc :data (.. ev -target -value)))
37
      (keyword name)
30
      (keyword name)
38
      nil)))
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
  (reify
41
  (reify
42
    om/IWillUpdate
43
    (will-update [_ p s] (prn p s))
52
    om/IRender
44
    om/IRender
53
    (render [_]
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
5
6
                 [com.datomic/datomic-free "0.9.4331"]
6
                 [com.datomic/datomic-free "0.9.4331"]
7
7
8
                 [om "0.1.5"]
8
                 [om "0.1.6"]
9
9
10
                 [ring "1.2.1"]
10
                 [ring "1.2.1"]
11
                 [compojure "1.1.6"]
11
                 [compojure "1.1.6"]
16
  :ring {:handler clarity.server/app}
16
  :ring {:handler clarity.server/app}
17
  :cljsbuild {:crossovers [clarity.types]
17
  :cljsbuild {:crossovers [clarity.types]
18
              :crossover-path "target/crossover"
18
              :crossover-path "target/crossover"
19
              :builds [{:source-paths ["cljs"]}]})
19
              :builds [{:source-paths ["om/src" "cljs"]}]})