Select to view content in your preferred language

map.on vs dojo/on

737
3
05-12-2014 11:08 AM
CurtisSpurlock
Emerging Contributor
map.on("load", doSomething);

on(map, "load", doSomething);

Is there a difference between the above statements.  I've seen both in the samples and I was curious.
0 Kudos
3 Replies
ReneRubalcava
Honored Contributor
For just listening for events, no.
Map extends dojo/Evented, so that allows you to do use it like map.on() to listen for events. It also allows you to use dojo/on to listen for events on the map.

The added benefit of using dojo/on is it provides a little more utility, like pausable handlers.

var handler = on.pausable(map, 'click', method);
handler.pause();
// some other stuff happens
handler.resume();

Or you could listen to events only once.
on.once(map, 'load', method);


So it all depends on what you are looking to do. I would recommend using the dojo/on method, simply for consistency as you will probably use it to listen for other events as well.
0 Kudos
CurtisSpurlock
Emerging Contributor
Great!  Thanks for the information.
0 Kudos
TracySchloss
Honored Contributor
I'm stuck with having to have my code work in IE 8, and I've had less problems with map.on syntax vs the on syntax.  I really wish we could get off it as our default browser, but for now we're stuck with it.  At least now they let users have something besides IE!
0 Kudos