plumatic / dommy

A tiny ClojureScript DOM manipulation and event library
759 stars 74 forks source link

allow set-attr! with a function value #26

Closed aiba closed 11 years ago

aiba commented 11 years ago

I have found it convenient to write template functions like this:

(for [x (some-list)]
  [:a {:onclick #(do-something x)} "click me to operate on" x])

Rather than serialize data in/out of the node and have a separate way of handling events.

Thus, I submit this pull request which allows dommy to generate nodes that work this way.

A possible disadvantage to this style of coding is that it leaves a bunch of function pointers (and their scopes) in memory. These functions should be garbage-collected with the dom node itself.

The advantage of coding this way is it's more concise and lets you store the context data of an event-handling function in a scope rather than serialized in the dom node itself.

jenanwise commented 11 years ago

Have you looked into delegated event handlers? It usually ends up being cleaner to define delegated event handlers using selectors (e.g. see jQuery.on's delegated events) rather than adding handlers directly to generated elements.

aiba commented 11 years ago

@jenanwise can you elaborate on the pros and cons of using a jQuery event, and what makes it "cleaner"? Maybe I'm missing something, but as I see it:

The disadvantages of a delegated event are:

The disadvantages of the direct function approach are:

Even if you put aside the performance considerations, I think it's often really useful to be able to express applications more concisely, which this pull request enables.