Event Delegation with Prototype and LowPro
Tumblr makes me put something here before a code block—
Event.addBehavior {
"button:click": ->
alert "A button was clicked."
this.disable().update "Clicked"
}
LowPro makes it easy to delegate events. The advantage of event delegation is that you can add to your event pool without adding individual listeners to many elements, greatly simplifying your code and making it more performant.
This also means that if you create a module or class for a specific purpose, like showing a dialog window, you can easily add events that are specific to your code, without interfering with your previously designed behaviors:
class Euclid.Window
initialize: (@element)->
@element.addClassName "EuclidWindow"
@addBehaviors()
addBehaviors: ->
Event.addBehavior {
".EuclidWindow button:click" ->
alert "And that button is inside an EuclidWindow"
}
show: ->
@element.appear()
Event.addBehavior.refresh()