OM: deref cursors in event handlers?

I am trying to figure out when we need to cancel the cursor to get its value.

The om cursors wiki says that event handlers are not considered part of the visualization phase, so the cursors in the handlers must be corrupted. The same is shown in the 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, handlers (onclick, onchange ...) use the cursor without separating it:

 (dom/button #js {:className "destroy" :onClick (fn [_] (put! comm [:destroy todo]))})) 

So what is the correct way?

Thanks.

+4
source share
1 answer

Note that delete and comm are not cursors, but core.async channels. Operation put! Adds a message to the channel that is being processed here .

Cursors are a way of wrapping state (called app-state in om). There are two things you can do with this condition:

  • Change the state: if you want to change the state of the application, you call om/transact! or om/update! on one cursor to application state (deref is never required). Om plans to display this transaction in the next rendering phase.
  • Reading state: when reading during the rendering phase (inside the render and render-state functions, the cursor works as its value, i.e. you do not need to separate it). At any other point in time, the cursor may be executed by a transaction or have a transaction with a schedule, so you want to cancel it in order to get the current value, and not some inconsistent state.
0
source

All Articles