This function will be called any time an event is added using the add() function and accepts the same parameters as the add() function. The first part of the function, ensures that the options exist and that the capture value is set. Capture is a required for the actual attachment of the event, but since it is false 99% of the time, we are making it optional. The wrapFn internal function is used to set the scope of, and pass through the data option into, the callback function. We then create an array of the important values for the cache and push it onto the stack.

We return the cache object so that the add() function can use wrapFn as its callback and apply the value of capture. This creates an interesting problem when removing events using the old remove function, as we have attached the the wrapFn as the callback, but code outside of this package, does not have access to the wrapping function. That is why we use a private remove function, so that we can locate wrapFn from the cache. The public remove function does just that:

Example 2: Public Remove function

The public remove function first gets all the cached objects for a given element that match the event type, using the getListeners() function (discussed later). We then iterate on the results and compare the paremeter fn with the cached non-wrapped function. When this is true, we actually remove the event, otherwise. The update function handles this removal:

Example 3: Removing Events and Cached Elements

First we actually remove the event with the internal remove() function (notice that the wrapped function is used). The rest of the function slices the cached event object that we are removing out of the stack.

In order to quickly and effectively find the events attached on an Element, both internally and externally, we created the public getListeners() function. I model the data architecture after a similar function in YUI, as I could not think of a more elegant way to return matching cache objects.

We simply iterate through the cache and create special result objects each time the element, and event type when provided, match that of a cached event object. Because the results objects can be passed externally, where the position name variables (EL, FN, TYPE, WFN, and OPT) do not exist, we instead create objects where each member is the lowercase of the constant and is set to the comparable value. The index is added, so the event object can later be removed from the cache.

I added two other functions that I found to be extremely useful. One is the removeEvents() function that removes all events from a given element by leveraging the existing caching infrastructure from event removal. And a removeAll() function that removes all events ever assigned using the add() function. This function is attached to the window.unload event and helps prevent memory leaks (especially in IE6) where the memory reference to the dom element that triggers the event is not cleared, because the event is still pointing to the dom element.

This Function will be called any time an event is added using the add method and accepts the same parameters as the add method. The first part of the Function, ensures that the options exist and that the capture value is set. Capture is a required for the actual attachment of the event, but since it is false 99% of the time, we are making it optional. The wrapFn internal Function is used to set the scope of, and pass through the data option into, the callback Function. We then create an array of the important values for the cache and push it onto the stack.

We return the cache object so that the add Function can use wrapFn as its callback and apply the value of capture. This creates an interesting problem when removing events using the old remove Function, as we have attached the the wrapFn as the callback, but code outside of this package, does not have access to the wrapping Function. That is why we use a private remove Function, so that we can locate wrapFn from the cache. The public remove Function does just that:

Example 2: Public Remove Function

The public remove Function first gets all the cached objects for a given element that match the event type, using the getListeners method (discussed later). We then iterate on the results and compare the paremeter fn with the cached non-wrapped Function. When this is true, we actually remove the event, otherwise. The update Function handles this removal:

Example 3: Removing Events and Cached Elements

First we actually remove the event with the internal remove Fucntion (notice that the wrapped Function is used). The rest of the method slices the cached event object that we are removing out of the stack.

In order to quickly and effectively find the events attached on an Element, both internally and externally, we created the public getListeners method. I model the data architecture after a similar Function in YUI, as I could not think of a more elegant way to return matching cache objects.

We simply iterate through the cache and create special result objects each time the element, and event type when provided, match that of a cached event object. Because the results objects can be passed externally, where the position name variables (EL, FN, TYPE, WFN, and OPT) do not exist, we instead create objects where each member is the lowercase of the constant and is set to the comparable value. The index is added, so the event object can later be removed from the cache.

I added two other Functions that I found to be extremely useful. One is the removeEvents Function that removes all events from a given element by leveraging the existing caching infrastructure from event removal. And a removeAll Function that removes all events ever assigned using the add method. This method is attached to the window unload event and helps prevent memory leaks (especially in IE6) where the memory reference to the dom element that triggers the event is not cleared, because the event is still pointing to the dom element.