Replacement for the unify! :post-fn

39 views
Skip to first unread message

Daniel Woelfel

unread,
Aug 9, 2012, 1:30:01 AM8/9/12
to c2-...@googlegroups.com
I'm trying to update some code to use the newest version of c2.

I've converted all of my

(unify! "div.my-selector" my-atom f)

with

(bind! "div.my-selector" 
  [:div.my-selector 
    (unify @my-atom f)])

Everything works well. It even somehow updates the dom when I swap! out my atom.

My problem is that I can't figure out how to replace the functionality of unify!'s :post-fn. My visualization has many points, and I used unify!'s :post-fn to create a kd-tree of all the points. This allows me to do efficient nearest-neighbor lookups when I want to show a tooltip.

Is there some event that is fired after the dom is updated? Are there any plans to add the post-fn functionality to singult's unify?

Kevin Lynagh

unread,
Aug 9, 2012, 2:00:06 AM8/9/12
to c2-...@googlegroups.com
Hi Daniel,

Right now there isn't a :post-fn on Singult's unify construct---I dropped that since it shouldn't be a concern of Singult or C2.
C2 is using the reflex library internally in the bind! macro to add callbacks to the atom(s) that're dereferenced within the body, which is how the updates work when you swap a new value into the atom.

The bind! macro returns a reflex "computed-observable" which implements IWatchable, so if you hold a reference to that and add a watcher, you can implement the old post-fn behavior manually.


My visualization has many points, and I used unify!'s :post-fn to create a kd-tree of all the points. This allows me to do efficient nearest-neighbor lookups when I want to show a tooltip.

If the data you need in the tooltip is the same data that is used to generate the node, you can pull it back out of the node using singult.core/node-data.
If you attach a delegated event handler to the container element, this will happen automatically for you.
For instance, if your container element has id "scatterplot", you can do:

    (c2.events/on "#scatterplot" :click (fn [d] (p (str "circle clicked:" (prn-str d)))))

 
Is there some event that is fired after the dom is updated?

I've been thinking about what an event bus would look like compared to the explicit references/callbacks used within reflex, but I'm not sure what that'll look like quite yet.
Paul DeGrandis's Shoreleave library has some fancy event bus stuff for ClojureScript, so you might want to check that out or talk with him on #clojure for ideas related to event buses.

best,

Kevin
Reply all
Reply to author
Forward
0 new messages