- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
YUI Bubbling Library: a seriously cool design pattern
Caridy Patino recently posted to the YUI Blog about his event-bubbling library, which uses the subscriber/publisher design pattern to abstract an entire webapp's event binding into its own unobtrusive behavioral layer. Instead of attaching events to individual DOM nodes using addListener, you intercept and process all events near the document root, then use CSS classes or other criteria to match up individual events with the correct handlers. In effect, you end up with a big, global switch statement for handling mouse clicks, mouseovers, keydowns and other events.
Patino, a respected contributor to the YUI mailing list, makes a strong case for the usefulness of his library on larger, more event-driven webapps. For one thing, it can reduce the overhead of handling IE memory leaks. For another, it can simplify the process of attaching custom JavaScript behaviors to dynamically loaded content. Best of all, it can be used to improve performance of really complex apps.
Of course, such an abstract approach isn't for every developer or every application. Patino is pretty frank about both the pros and cons of his approach for specific situations. Even if the actual technique isn't for you, though, the post is a fantastic primer on the intricacies of DOM events and the publisher/subscriber pattern.
Topics: Design Patterns, Javascript, Javascript Libraries, Patterns
Comments: 2 so far
Leave a comment
About Pathfinder
Recent
- Thanksgiving 2008: What We’re Thankful For (In Rails)
- iPhone SDK: Testing with TextMate & GTM
- GWTQuery - JQuery-like Syntax in GWT
- Ask the readers: How do I fire native browser events in Prototype.js?
- News Rollup for the Week of November 17, 2008
- Rails ThreatDown!
- Automated Deployments Rock
- Bandwidth profiling Flex projects and more with Charles
- iPhone SDK: UIViewController Testing & TDD
- Icons are evil; so are menus - unless you do them right
Archives
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006


The Bubbling Library is absolutely awesome.
Now, everyone that uses Behaviour/EventSelector with Prototype/Scriptaculous can’t say, “YUI sucks because you end up writing too much code for each DOM element”.
Kudos to Caridy.
By the way, event-driven application development like this using css selectors isn’t faster. It always has to keep crawling the DOM–but what if you update the DOM dynamically/periodically? You’ll need to keep crawling the DOM again and re-attach the listeners. Yes, you write less lines of code–but you end up crawling the DOM too much–which gets CPU intensive (IE6 cries like a baby when crawling, Safari, Firefox, Opera and IE7 can crawl nicely).
Comment by MezZzeR, Sunday, September 30, 2007 @ 3:03 am
YUI sucks because i can’t get tables to sort :), the documentation is unreadable. Apart from being completely useless it’s pretty nice
Comment by francine, Monday, June 9, 2008 @ 4:18 am