I am seeing the following error when trying to update a cursor:
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)));
I am using a ref-cursor to point to the comments-data vector in my application state:
(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))))
Now, I would like to update this cursor when a user submits some data via an input in my input component. I use (let [foo (->> comments)] to refer to the cursor inside render-state. I pass it into a function call that handles the user submit and I would like to simply add one more item to this vector.
(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"))))))
However, when I make the om/transact! call I am seeing the above error.
You want to call your
comments
function and use its return value (the cursor), not use the function itself. Change this line:to:
and that should work for you.