clojurescript - Om ref cursor not re-rendering components when updated -
(ns ^:figwheel-always refs-test.core (:require [om.core :as om :include-macros true] [om.dom :as dom :include-macros true] [sablono.core :as html :refer-macros [html]])) (enable-console-print!) (def app-state (atom {:items [{:text "cat"} {:text "dog"} {:text "bird"}] :selected-item {}})) (defn selected-item [] (om/ref-cursor (:selected-item (om/root-cursor app-state)))) (defn selected-item-title [_ owner] (reify om/irender (render [_] (html [:div (let [selected (om/observe owner (selected-item))] (if (empty? selected) [:h1 "nothing selected"] [:h1 (:text selected)]))])))) (defn selected-item-button [item owner] (reify om/irender (render [_] (html [:li [:button {:on-click (fn [] (om/update! (om/root-cursor app-state) :selected-item item) ;; doesn't update ;;(om/update! (om/root-cursor app-state) :selected-item (merge item {:foo 1})) ;; )} (:text item)]])))) (defn root [cursor owner] (reify om/irender (render [_] (html [:div (om/build selected-item-title {}) [:ul (om/build-all selected-item-button (:items cursor))]])))) (om/root root app-state {:target (.getelementbyid js/document "app")})
(https://www.refheap.com/108491)
the (selected-item)
function crerates ref-cursor tracks :selected-item
key in app-state
. when click selected-item-button
title changes reflect new value has been put map. however, works once. pressing different button not cause title re-render again title stuck @ value of first button pressed.
although, adding merge
additional keyword seems make work... (merging empty map doesn't work either, tried that!)
is understanding on ref cursors wrong?
so, issue simple.
(om/update! (om/root-cursor app-state) :selected-item item)
should have been
(om/update! (om/root-cursor app-state) :selected-item @item)
notice item, because it's cursor, dereferenced.
Comments
Post a Comment