Skip to content

Instantly share code, notes, and snippets.

@calvis
Created August 11, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvis/31d895a924b888e8fff0e89d1cd7d8aa to your computer and use it in GitHub Desktop.
Save calvis/31d895a924b888e8fff0e89d1cd7d8aa to your computer and use it in GitHub Desktop.
(ns frustration.core
(:require [goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom :include-macros true]))
(enable-console-print!)
(def init-data
{:list [{:id1 "temp"}
{:id1 "other"}
{:id1 "etc"}]})
(defui ThingWithIdent
static om/Ident
(ident [this {:keys [id1]}]
[:id2 id1])
static om/IQuery
(query [this]
'[:id1])
Object
(render [this]
(dom/div {} (str (:id1 (om/props this))))))
(def thing (om/factory ThingWithIdent {:keyfn :id1})) ; this has to be :id1 or things don't render right
(defui RootView
static om/IQuery
(query [this]
[{:list (om/get-query ThingWithIdent)}])
Object
(render [this]
(let [{:keys [list]} (om/props this)]
(apply dom/div {} (map thing list)))))
(defmulti read om/dispatch)
(defmethod read :list
[{:keys [state query] :as env} key params]
(let [st @state]
{:value (om/db->tree query (get st key) st)}))
(defmulti mutate om/dispatch)
(defmethod mutate 'app/tempids
[{:keys [state]} _ {:keys [name]}]
{})
;; :id2
(def reconciler2
(om/reconciler {:state init-data
:id-key :id2 ; intuitively chose :id2 but this behaves badly
:parser (om/parser {:read read :mutate mutate})}))
(om/add-root! reconciler2
RootView
(gdom/getElement "app"))
(om/merge! reconciler2 {'app/tempids {:tempids {[:id2 "temp"] [:id2 "new"]}}})
(println @reconciler2)
;; => {:list [[:id2 temp] [:id2 other] [:id2 etc]],
;; :id2 {temp {:id1 temp}, other {:id1 other}, etc {:id1 etc}},
;; :om.next/tables #{:id2}}
(om/remove-root! reconciler2
(gdom/getElement "app"))
;; :id1
(def reconciler1
(om/reconciler {:state init-data
:id-key :id1 ; not :id2
:parser (om/parser {:read read :mutate mutate})}))
(om/add-root! reconciler1
RootView
(gdom/getElement "app"))
(om/merge! reconciler1 {'app/tempids {:tempids {[:id2 "temp"] [:id2 "new"]}}})
(println @reconciler1)
;; => {:list [[:id2 new] [:id2 other] [:id2 etc]],
;; :id2 {new {:id1 new}, other {:id1 other}, etc {:id1 etc}},
;; :om.next/tables #{:id2}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment