Skip to content

Instantly share code, notes, and snippets.

@holyjak
Last active February 10, 2020 09:47
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 holyjak/c0f2cbf515b2e10191aee516e2553922 to your computer and use it in GitHub Desktop.
Save holyjak/c0f2cbf515b2e10191aee516e2553922 to your computer and use it in GitHub Desktop.
Fulcro mystery - why is initial state not normalized in the DB?
(defsc Person [this {:person/keys [name] :as props}]
{:query [:ui/modified? :person/id :person/name :address/city :address/state]
:ident :person/id
:initial-state (fn [{:keys [id name city state]}] {:person/id id
:person/name name
:address/city city
:address/state state})}
(dom/li
(dom/h5 (pr-str props))))
(def ui-person (comp/factory Person {:keyfn :person/id}))
(defsc Root [this {people :people}]
{:query [:people]
:initial-state {:people [(comp/get-initial-state Person {:id 0 :name "Sally" :city "Nowhere" :state "GA"})
(comp/get-initial-state Person {:id 1 :name "Tom" :city "There" :state "OH"})]}}
(dom/section
(dom/h3 "Peeps")
(dom/ul
(map ui-person people))))
;; Client DB:
;;{:people
;; [{:person/id 0,
;; :person/name "Sally",
;; :address/city "Nowhere",
;; :address/state "GA"}
;; {:person/id 1,
;; :person/name "Tom",
;; :address/city "There",
;; :address/state "OH"}],
;; :fulcro.inspect.core/app-id "app.ui.tmp-people/Root",
;; :fulcro.inspect.core/app-uuid
;; #uuid "e0e80144-205a-47af-b08a-bd0dd29a8967",
;; :com.fulcrologic.fulcro.application/active-remotes #{}}

Solution

There are 2 problems:

  1. L14 should have been {:query [:people (comp/get-query Person)]
  2. L15 mixes the template and lambda forms; it should have been either
:initial-state (fn [_] {:people [(comp/get-initial-state Person {:id 0 :name "Sally" :city "Nowhere" :state "GA"})
                                 (comp/get-initial-state Person {:id 1 :name "Tom" :city "There" :state "OH"})]}

or a full template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment