I was thinking about this the other day as I was looking at derbyjs, which has an interesting way of handling this - they attach all event listeners to the document element and manually bubble the event from event.target until it hits a matching handler.
2 interesting things:
1. bubbling automatically stops as soon as you hit a handler, so they provide a "next" callback if you'd like to continue bubbling after handling an event
2. if an element doesn't have an id, they assign it a unique one with a custom prefix (this is because they map elements to handlers via the id attribute).
The advantage is that you get event delegation while still declaring event handlers exactly as you'd do now.
I'll try to put together a proof-of-concept sometime soon (but my time right now is constrained), but here's a quick sketch of what this could look like:
- define an angular module to contain a "$delegate" service and the various specific event-handling directives that will be necessary
- a directive like "click" would obviously depend on the $delegate service, and when called on an element, would:
1. add an id if necessary
2. call something like $delegate("click", id, fn), where fn would be the scope fn specified by the user
- and then it's the $delegate's job to attach the handler to the document element and dispatch the event accordingly
On Monday, October 1, 2012 5:49:28 PM UTC-5, Nish Patel wrote: