On Sunday, August 9, 2015 at 10:50:57 AM UTC+3, Gary Verhaegen wrote:
> You deref if you want to get the current value. The argument to om/update!, 
> however, must be a cursor, not a random value.
> 
> 
> In this particular case, the code uses om/update! because it does not need to 
> use the old value to compute the new one. Since the code of submit does not 
> seem to require the value at the cursor, there is no need to deref.
> 
> On Sunday, 9 August 2015, Asher Coren <[email protected]> wrote:
> Hello,
> 
> 
> 
> I'm trying to understand when we must deref a cursor to get its value.
> 
> 
> 
> In the om cursors wiki (https://github.com/omcljs/om/wiki/Cursors) it states 
> that
> 
> event handlers are considered not part of the render phase, and therefore 
> cursors in handlers should be drefed.
> 
> Same is shown in the Basic-tutorial 
> (https://github.com/omcljs/om/wiki/Basic-Tutorial):
> 
> 
> 
> (defn contact-view [contact owner]
> 
>  (reify
> 
>    om/IRenderState
> 
>    (render-state [this {:keys [delete]}]
> 
>      (dom/li nil
> 
>        (dom/span nil (display-name contact))
> 
>        (dom/button #js {:onClick (fn [e] (put! delete @contact))} 
> "Delete")))))
> 
> 
> 
> But, in the TodoMVC code 
> (https://github.com/swannodette/todomvc/blob/gh-pages/labs/architecture-examples/om/src/todomvc/item.cljs),
>  the handlers (onclick, onchange...) use the cursor without drefing it:
> 
> 
> 
> (defn submit [e todo owner comm]
> 
>  (when-let [edit-text (om/get-state owner :edit-text)]
> 
>    (if-not (string/blank? (.trim edit-text))
> 
>      (do
> 
>        (om/update! todo :title edit-text)
> 
>        (put! comm [:save todo]))
> 
>      (put! comm [:destroy todo])))
> 
>  false)
> 
> 
> 
> So, what is the correct way?
> 
> 
> 
> Thank you.
> 
> 
> 
> --
> 
> Note that posts from new members are moderated - please be patient with your 
> first post.
> 
> ---
> 
> You received this message because you are subscribed to the Google Groups 
> "ClojureScript" group.
> 
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> 
> To post to this group, send email to [email protected].
> 
> Visit this group at http://groups.google.com/group/clojurescript.

Gary,
Thank you for your reply. I realize now that I pasted the wrong part of code. 
Your explanation is correct with regards to `om/update!`. But, later in the 
code we have:
    (dom/button
      #js {:className "destroy"
      :onClick (fn [_] (put! comm [:destroy todo]))}))

Here again we send the cursor itself (`todo`) without derefing. When reading 
from the channel, it is used like this:
(defn destroy-todo [app {:keys [id]}]
  (om/transact! app :todos
    (fn [todos] (into [] (remove #(= (:id %) id) todos)))
    [:delete id]))

Here we require the value of the cursor ({:keys [id}) - but we sent it without 
derefing!

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.

Reply via email to