A sequence of cursors for Om build-all, but need additional app-state info as well

187 views
Skip to first unread message

Andrew Stoeckley

unread,
Aug 4, 2014, 7:02:12 AM8/4/14
to clojur...@googlegroups.com
As Om's build-all takes a sequence of cursors, I'm wondering the best practice for when the component function passed to build-all needs to lookup its item from the sequence to get more info about it from a different cursor in the app-state, i.e. in a relational database type of way:

(defn app-state
(atom {:attributes {... map of an id to info about that id ...}
:id-sequence [id1 id5 id4 id7... etc as if based on a sorting order or other sequence, perhaps even repeated items]}))

Now suppose I want to call build-all on :id-sequence but then use info from :attributes after getting an item from the vector?

One bloated option would be to create a new sequence in the app state that holds the id and its attributes, which would mean for repeated ids the same attributes are stored multiple times.

Unlike other Om components, I'm guessing build-all is not designed to accept multiple cursors since it needs a single sequence to do its thing.

Can anyone advise a good approach?

Steve Ashton

unread,
Aug 4, 2014, 11:26:10 AM8/4/14
to clojur...@googlegroups.com
I'm new to Om, but here's what I came up with.

(def app-state
(atom
(:attrs {1 "one" 2 "two" 3 "three" 4 "four"}
:ids [1 2 3 3 4 2]}))

(defn number-view [[id attrs] _]
(reify
(om/IRender
(render [this] (dom/li nil (get attrs id)))))

(cursor-tuples [state]
(map vector (:ids state) (repeat (:attrs state))))

(defn numbers-view [app _]
(reify
om/IRender
(render [this]
(dom/div nil
(apply (dom/ul nil
(om/build-all number-view (cursor-tuples app)))))))


Apologies for any typos, typing from a mobile device.

Johann Bestowrous

unread,
Aug 4, 2014, 11:33:58 AM8/4/14
to clojur...@googlegroups.com
You can always use regular maps to build components. Here's an example from David Nolen that might help: https://github.com/swannodette/om/blob/master/examples/sortable/src/core.cljs#L259

He maps over an id-sequence just like would want. Let me know if you have any follow up questions.

Andrew Stoeckley

unread,
Aug 4, 2014, 12:13:42 PM8/4/14
to clojurescript
Ah, what I failed to realize is that you can generate a sequence of
cursors inline from other cursors. I had previously thought that the
cursor sequence must exist in the app state.
> --
> Note that posts from new members are moderated - please be patient with your first post.
> ---
> You received this message because you are subscribed to a topic in the Google Groups "ClojureScript" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojurescript/O99WL6rrhO4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to clojurescrip...@googlegroups.com.
> To post to this group, send email to clojur...@googlegroups.com.
> Visit this group at http://groups.google.com/group/clojurescript.
Reply all
Reply to author
Forward
0 new messages