LA.NET [EN]

Oct 07

If you’ve use MS AJAX in the past, then you’ve probably used the $addHandler helper method. As you might recall, this method was introduced with version 1 of the library and its main purpose was to simplify the code needed for subscribing DOM event (and yes, as you might expect, it hid all those gory details associated with DOM event handling in different browsers).

The latest release adds one parameter to the existing definition. If you look at preview 5, you’ll see that it now looks like this:

function Sys$UI$DomEvent$addHandler(
element,
eventName,
handler,
autoRemove)

The first three parameters are the same as the ones we had in v1.0. What about the autoRemove parameter? Well, that’s new :,,)

This parameter lets you configure an “auto-removable handler” (I’ve just made up this name, so I guess this is not its official designation :)). When you set autoRemove to true, you’re saying that the handler should also be responsible for cancelling that subscription. But when should that cancellation happen? Unline JQuery, which has a one time handler which is responsible for removing that handler after that one time event handling, with MS AJAX the handler will only be removed during the disposing phase (thanks to Dave Reed for explaining this to me because initially I could only see it as badly coded JQuery one time event clone!)

This will help you in several scenarios which, in previous releases, might lead to a memory leak. For instance, suppose you’ve got a DataView control and you use this method for hooking up an event from one of its child nodes. Before having this parameter, refreshing the data meant that you had to go through the DataView’s child nodes and cancel all existing event handlers. With this parameter, the only thing you need to do is set it to true when you’re hooking up the events.

Moral of the story: if you’re building an AJAX page with MS AJAX components that need to refresh themselves and you’re also using event handlers for processing events fired from “within those components”, then  you should set this parameter to true to ensure that the handler will be disposed of when a refresh happens (because generally that means that a dispose will be generated).

And that’s it for today. Keep tuned from more on MS AJAX.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>