1

カーソルを更新しようとすると、次のエラーが表示されます。

Uncaught Error: No protocol method ITransact.-transact! defined for type function: function comments(){return om.core.ref_cursor.call(null,new cljs.core.Keyword(null,"comments-data","comments-data",1871210833).cljs$core$IFn$_invoke$arity$1(om.core.root_cursor.call(null,cljs_playground.core.app_state)));

アプリケーション状態のコメント データ ベクトルを指すために ref-cursor を使用しています。

(def app-state
  (atom
    {:comments-data [{ :author "Commenter 1" :text "comment 1" }
                     { :author "Commenter 2" :text "comment 2" }]}))

(defn comments []
  (om/ref-cursor (:comments-data (om/root-cursor app-state))))

ここで、ユーザーが入力コンポーネントの入力を介してデータを送信したときに、このカーソルを更新したいと思います。(let [foo (->> comments)] を使用して、render-state 内のカーソルを参照します。それをユーザー送信を処理する関数呼び出しに渡し、このベクトルに項目をもう 1 つ追加したいと思います。

(defn handle-submit [e owner {:keys [text]} foo]
  (om/transact! foo #(concat % {:author "foo" :text "bar"}))
  (om/set-state! owner :text ""))

(defn Input
  [data owner]
  (reify
    om/IInitState
    (init-state [_]
      {:text nil})
    om/IRenderState
    (render-state [this state]
     (let [foo (->> comments)]
       (dom/div nil
         (dom/input #js
           { :type "text"
             :ref "text-field"
             :value (:text state)
             :onChange (fn [event] (handle-change event owner state))})
         (dom/button #js 
           { :onClick (fn [event] (handle-submit event owner state foo))} "submit"))))))

ただし、om/transact を作成すると! 上記のエラーが表示されます。

4

2 に答える 2

1

変化する

(let [foo (->> comments)]

(let [foo (comments)]

よりシンプルに保ちます。->>オペレーターは必要ありません。

于 2015-04-19T03:59:41.037 に答える