FishBarFood
FishBarFood

Reputation: 51

Removal of Dojo Event Handlers

I'm not sure whether or not i need to remove any event handlers i've attached, or whether Dojo/JS will automagically clear my crap up after me...

Let's say i have two "Pages", both loaded via their own modules. When Page 1 is loaded, i display 6 buttons all sharing an on event using the on(element,event name,handler) pattern. When i move to Page 2, the relevant Page 1 nodes are destroyed, new Page 2 nodes get loaded with different buttons and on handler(s). Is there a requirement/benefit to ensuring that when i leave a "Page" that the event handlers created are all cleaned up behind me?

I've read this page, Events with Dojo, from which i originally took away that on events follow the pattern: on(element,event name,handler).

However, in considering this issue, i noticed the example:

var handle = on(myButton, "click", function(evt){
    // Remove this event using the handle
    handle.remove();
    ...

So, i guess, distilling the above, my questions are:

  1. Do i need to worry about cleaning up subsequently inactive on events?

  2. And if 1. == yes, is my only option to create references to the events?

Upvotes: 1

Views: 940

Answers (1)

GibboK
GibboK

Reputation: 73918

Use this.own() from dijit/Destroyable

The own() function tracks specified handles and remove/destroy them when this instance is destroyed, unless they were already removed/destroyed manually.

More infos here: http://dojotoolkit.org/reference-guide/1.10/dijit/Destroyable.html `

Upvotes: 2

Related Questions