fulcro

Book: http://book.fulcrologic.com, Community Resources: https://fulcro-community.github.io/, RAD book at http://book.fulcrologic.com/RAD.html
tony.kay 2021-06-29T02:15:24.307300Z

Not sure what you mean. An attribute is allowed to hold any arbitrary EDN as data. So, it is perfectly legal to just put your hiccup-like thing in state, and query for it as a single attribute. Does that help, or doyou mean something else?

tony.kay 2021-06-29T02:19:03.307500Z

(defsc MyDynamicThing [this {:thing/keys [hiccup]}]
 {:query [:thing/id :thing/hiccup]
  :ident :thing/id
  :initial-state {:thing/id 1 :thing/hiccup [:div "Hello"]}}
 (sablono.core/html hiccup))

tony.kay 2021-06-29T02:24:35.307700Z

If you're instead trying to build an HTML-like model where each DOM component node is represented as a Fulcro component, then that is a bit more involved. You could generate something like this:

(declare ui-html-node)
(defmulti render-node :node/type)

(defmethod render-node :div [node]
  (dom/div
    (mapv ui-html-node (:node/children node))))

(defsc HTMLNode [this props]
  {:query (fn [] [:node/id :node/type :node/text {:node/children ...}])
   :ident :node/id}
  (render-node props))

(def ui-html-node (comp/factory HTMLNode {:keyfn :node/id}))
or something like that...basically the multi-method does the detail rendering for each node, but using the (single recursive) HTMLNode during recursion.

tony.kay 2021-06-29T02:26:41.308Z

I don't recommend this latter method, actually. I worked on a project that used that approach, and it became quite complex tracing down the data model, reloading the mess, etc. The RAD approach for dynamism is much more amenable I think. You can generate such components on-the-fly and then only customize the data model definition, instead of then entire UI.

lgessler 2021-06-29T04:01:54.308200Z

ah sorry I wrote that sloppily--I meant the latter, where I make some TreeNode that will recursively call itself for the hiccup-y structure, pretty much what you have there, the only issue being that the nodes don't have :node/id after they've been freshly loaded from the server, and there's no easy way to construct one from just the data in the nodes

lgessler 2021-06-29T04:22:53.308900Z

I guess my question is, if I used an approach like the one you outline, where the right place would be to assign a random ID (like a UUID) to every node. Perhaps a pre-merge function in HTMLNode that assigns a :node/id if it's not present

lgessler 2021-06-29T04:25:26.309200Z

good to know RAD handles this more gracefully though--more reason for me to sit down and learn it