How to disable an "on" event?

7085
6
Jump to solution
04-12-2016 06:04 AM
PavelVeselský1
New Contributor III

The only way I know to disable (or better, remove, I don't need to re-enable it) an on event in dojo is to use a handle, like this:

var myUnload = map.on("unload", unloadHandler); 
myUnload.remove(); 

In Web AppBuilder, event handlers are supposed to be made through this.own, at least this method is used in all the widgets by ESRI. How to disable such events? Setting a handle doesn't seem possible.

What exactly does this.own do? If I understand it right, it should save the event somewhere into the widget. But where? How can I remove it from there?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Pavel,

  OK, I slowed down and re-read your original post a couple of time and I think what you are wanting is achieved by this code:

this.own(this.mapClickEvent = this.map.on('click', lang.hitch(this, this.mapClickEvtHandler)));

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

Pavel,

  This.own is a way for a widget to destroy/remove events when the widget it destroyed.

The advantage of using own() in the widget infrastructure is that internally, the widget can keep track of all of its handles, and make sure everything is disconnected and/or unsubscribed when the widget is destroyed—preventing any kind of memory leaks.

You can use JS removeEventListener in modern browsers if you know the event and function that the event is calling:

node.removeEventListener ("click", clickFunction, false);

PavelVeselský1
New Contributor III

I can't get it to work - the event is over this.map which is not a DOM node and thus doesn't contain removeEventListener, or misses the right node (I tried this.map.container.removeEventListener("click", this.onMapClick, true);). The function this.onMapClick still runs when it shouldn't.

But thanks for pointing to this function, Robert!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Pavel,

  I doubt that it would work but,

this.map.domNode.removeEventListener("click", this.onMapClick, true);

may be worth a try.

0 Kudos
KenBuja
MVP Esteemed Contributor

Can you use the pausable method of "dojo/on", since its target doesn't need to be a DOM node?

...the target object (a DOM node or other event emitting object) that will be the source of the event. The target object may be a host object with its own event capabilities (like DOM elements or the window), or it may be a JavaScript object with an on() method.

PavelVeselský1
New Contributor III

It needs some handler which I didn't have, so I accepted Robert's answer as "correct". In practice, I will use your answer as well, reusing listeners is a good practice and my case it will be very helpful. Thanks, Ken!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Pavel,

  OK, I slowed down and re-read your original post a couple of time and I think what you are wanting is achieved by this code:

this.own(this.mapClickEvent = this.map.on('click', lang.hitch(this, this.mapClickEvtHandler)));

0 Kudos