dojo/on versus dojo.connect

5458
9
Jump to solution
10-24-2012 12:05 AM
StephanMendler
New Contributor III
Hi,

when i try to substitute

dojo.connect(map, "onLoad) function(){
  // do something;
};

with

require(["dojo/on"], function(on){
    on(map, "onLoad", function(){
      // do something;
    });
});

Dojo throws an error:
Target must be an event emitter

Any ideas?

Regards
Stephan
0 Kudos
1 Solution

Accepted Solutions
__Rich_
Occasional Contributor III
I don't think (ESRI) JS API objects are compatible with dojo.on yet.

http://forums.arcgis.com/threads/65179-Does-the-JavaScript-API-support-event-handling-via-quot-on-qu...

dojo.on is dependent on some specific mixins etc. and if you have a look at the Dojo source you'll see that exception is only thrown after a whole bunch of checks and initialisation has been performed.

View solution in original post

0 Kudos
9 Replies
__Rich_
Occasional Contributor III
I don't think (ESRI) JS API objects are compatible with dojo.on yet.

http://forums.arcgis.com/threads/65179-Does-the-JavaScript-API-support-event-handling-via-quot-on-qu...

dojo.on is dependent on some specific mixins etc. and if you have a look at the Dojo source you'll see that exception is only thrown after a whole bunch of checks and initialisation has been performed.
0 Kudos
StephanMendler
New Contributor III
Yes, i think your are right. In the moment there is a lot of new dojo functionality (dojo/on and the new AMD pattern etc.) which is not yet (or just partially) implemented in the current Javascript API.
Thanks for your answer.
0 Kudos
JimSomerville
New Contributor II
This seems to still be the case.  We are working on creating a Dojo 2.0, fully AMD-compatible application.  The documentation states that in addition to "dojo/on", "dojo/aspect" is another option.  And event handlers, using dojo/aspect, do get called.  However, the handler arguments are not available.  For example, in the snippet below, "layer" is undefined:

aspect.after(this.map, "onLayerAddResult", lang.hitch(this, function (layer, error) {

}));
0 Kudos
ReneRubalcava
Frequent Contributor
Connect and Aspect work off execution of method. So in the API, there is a method called onLayerAddResult() that is executed.
Here is an older explanation of how these work.
http://dojotoolkit.org/documentation/tutorials/1.6/events/ <- added a better reference

In order for dojo.on to capture an event, the object, in this case the "map" would need to extend dojo/Evented and emit a custom event in the code somewhere by doing something like
this.emit('onLayerAddResult', {layer:layerObject, error:errorMessage})

This still isn't 100% in the API yet.
At the moment, esri.Map does extend dojo/Evented. You can test it by doing this.
map.on('click', function(e) {console.log(e)});
It just doesn't emit all the custom events yet, but it works for DOM events.
0 Kudos
JoshuaFix
New Contributor
just a side note, make sure when using on to remove "on" from the actual event name.  example:

on(map, "onLoad", function(){

should really be

on(map, "Load", function(){
0 Kudos
JasonZou
Occasional Contributor III
Can any ESRI staff answer the question? In what scope the JS API is compatible with dojo/on? Does ESRI have a timeline to fully support dojo/on and other dojo 1.8 features? It is annoying to mix using dojo.connect and dojo/on in the same application.

Thank you,
Jason Zou
0 Kudos
derekswingley1
Frequent Contributor
Can any ESRI staff answer the question? In what scope the JS API is compatible with dojo/on? Does ESRI have a timeline to fully support dojo/on and other dojo 1.8 features? It is annoying to mix using dojo.connect and dojo/on in the same application.


As of version 3.4, classes in the JS API are not compatible with dojo/on. The exception is the Map class but we haven't doc'ed this because we want to be able to say we support dojo/on across the entire API rather than doing it piece by piece. We are still working on it...

When it's finished, we'll encourage you to do object.on("event-name", callback); rather than on(object, "event-name", callbcak); but that will be up to you.
0 Kudos
JasonZou
Occasional Contributor III
Thank you Derek for your reply. I can tell ESRI is working very hard to make it compatible to AMD 'cause many times when I look at the API, there are some classes starting support dojo/on event style.

Derek, may I ask the reason for the recommendation to use object.on instead of on(object, ..)? Is that because object.on will register the event handler at the object scope, while on(object, ...) will register at the global level?

Thank you,

Jason Zou
0 Kudos
derekswingley1
Frequent Contributor

Derek, may I ask the reason for the recommendation to use object.on instead of on(object, ..)? Is that because object.on will register the event handler at the object scope, while on(object, ...) will register at the global level?


Using on(object, event, callback) shouldn't be any different than using object.on(event, callback). My recommendation was purely from a readability point of view�?? I find object.on style code easier to read.

Also note that as of version 3.5 esri/map and everything in esri/layers and esri/tasks now has .on style events.
0 Kudos