While I can’t honestly say I have spent the time to compare Yahoo’s efforts to those of others, such as prototype or script.aculo.us, I have taken a liking to the way the Yahoo library feelsâ€¦ even if it’s just their coding style just feels a little funky.
One of the staple favourites of the library, is the Drag-Drop collection. The boss (and the client) always likes it when they can drag things around their web applications, especially when it’s not just pointless eye candy. After playing with the scripts for a while, I got the feeling that something was missing. It turned out to be the ability to subscribe to drag drop events. The general way Yahoo deals with the “event” handlers for the dragged or dropped items is by overriding functions that are called. This works quiet well for most things, but seeing as another important part of the library is event handling, including custom events, it seems a shame to let it go to waste and not allow events to be fired and subscribed to by the Drag-Drop classes.
With that in mind, and borrowing some concepts from an inspiring article I wrote a simple extension to the YAHOO.util.DD class that creates custom events that can be subscribed to by any number of listeners.
There were some issues with the apparent inconsistency of the arguments being passed between functions, but I normalised it as best I could and can up with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
It seems so obvious now. I’ve put together a basic example to show how this can be used.
This is only a start, there are plenty of other events that are worth overriding in this manner, for example the onDragEnter, onDragOut, onDragDrop, onDragOver, onMouseUp and onMouseDown functions (and a few more). That’s another thing I like about the YUI, it’s designed to be extended.