Closed aiba closed 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.
@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.
I have found it convenient to write template functions like this:
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.