Blog

Don’t forget addEvent!

After reading Dustin’s post about forgetting addEvent, I have to disagree.
Not with the part about using Yahoo’s new Event Utility (that part I DO agree with), the part I disagree with is dropping of the addEvent function.

You see, most folks already have some form of addEvent, with all of the standard params, so why force them to go through their code, and replace addEvent with "YAHOO.util.Event.addListener"?

The easier solution is to take your current addEvent function, and make it a wrapper for the new Yahoo event utility, like so:


function addEvent(el, sType, fn, oScope, bOverride){
YAHOO.util.Event.addListener
(el, sType, fn, oScope, bOverride);
}

Now you have an easy way of integrating it into your current scripts without having to rewrite all of your code.

Perhaps a bit obvious to some, but perhaps helpful to others :)

2 comments to “Don’t forget addEvent!”

  1. Tom
    01

    What?

  2. Dustin Diaz
    02

    Indeed, don't forget it. But just so you know, there's a nice little (nifty) shortcut (only by a couple of characters). You can user YAHOO.util.Event.on() which does the same stuff as addListener. Other than that, namespacing in general is a good idea. It avoids conflicts when you end up having to share you code… or rather, if someone wants to use the code from your site, you can be sure that it won't be conflicting with someone elses. If none of that makes sense, just be happy that Yahoo has done us all the favor of putting everything under the YAHOO namespace.

Comments are closed.