How to turn tool for mapclick event on and off

2050
3
Jump to solution
05-11-2018 10:05 AM
ElizabethMiller
New Contributor III

When my custom widget opens, I have a map onclick event enabled that turns on with no problem from a toggle button. The user then clicks on the map and gets some info based on the click. But I can't turn the functionality off again. When the button is toggled off the mapclick still works. Even after the widget container is closed, the mapclick event is still active. 

I've tried using a handle with dojo.connect and dojo.disconnect but that hasn't worked for me.

Can someone help? 

Basic widget stuff but I'm not getting it.

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Elizabeth,

   To start you should not use dojo.connect it replaced by dojo on.

As of Dojo 1.7, the preferred way of handling events is to use the new lightweight dojo/on module.

For compatibility reasons, the dojo.connect api’s will remain fully supported through remaining 1.x releases, but will likely be removed in Dojo 2.0. Migration from connect() to on() should be straightforward as the api signature is very similar.

If you create a handle in your widget for the map on click event you can remove the event listener by using mapClickEvt.remove() on widget close and or deactive.

The proper way to do this in code is:

this.own(this.mapClickEvtHandler = this.map.on('click', lang.hitch(this, function(){
   //your function code
})));

onClose: function () {
  this.mapClickEvtHandler.remove();‍‍‍‍‍‍‍‍‍‍
},

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Elizabeth,

   To start you should not use dojo.connect it replaced by dojo on.

As of Dojo 1.7, the preferred way of handling events is to use the new lightweight dojo/on module.

For compatibility reasons, the dojo.connect api’s will remain fully supported through remaining 1.x releases, but will likely be removed in Dojo 2.0. Migration from connect() to on() should be straightforward as the api signature is very similar.

If you create a handle in your widget for the map on click event you can remove the event listener by using mapClickEvt.remove() on widget close and or deactive.

The proper way to do this in code is:

this.own(this.mapClickEvtHandler = this.map.on('click', lang.hitch(this, function(){
   //your function code
})));

onClose: function () {
  this.mapClickEvtHandler.remove();‍‍‍‍‍‍‍‍‍‍
},
ElizabethMiller
New Contributor III

Once again you have helped me with exactly what I needed. Thank you!

Where should I have looked for documentation or examples of this that I could have found on my own?

Thanks again.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Various Dojo documentation as this involves several classes, but mainly dojo/on — The Dojo Toolkit - Reference Guide . But looking at esri OTB widget is great place too. You will see that they use this.own all throughout their widget an only use dojo on.