Reputation: 1209
I have a class that inherits dojo/Evented. I call the myClass.emit("load",{})
in my code. and listen to it by myClassInstance.on("load",function(){console.log("fired!")});
However, it's not fired. But interestingly, if I change to myClass.emit("myClass-load",{})
and listen by myClassInstance.on("myClass-load",function(){console.log("fire")});
the event get fired!
What did I miss here? Is it because other objects also listen to "load" event so it's not caught by myClassInstance?
Generally, what happens if two events, fired by two objects have the same name? Is it better to name each event uniquely?
Thanks, Allen
Upvotes: 0
Views: 168
Reputation: 10559
If your class is inheriting dijit/_WidgetBase
, there are a couple of differences in its treatment of on
and emit
that I might suspect are causing your issue.
Firstly, _WidgetBase won't emit DOM events before the widget has startup
called or after/while it is destroyed. In other words, it won't emit DOM events during its constructor
, postscript
, buildRendering
, and postCreate
.
Secondly, _WidgetBase contains some admittedly convoluted logic for backwards compatibility with legacy widgets which implement events via methods following the onXxx
naming pattern. While _WidgetBase#emit
will still fire a DOM event even if a matching onXxx
method exists, _WidgetBase#on
will not listen for the DOM event if it finds an onXxx
method first.
Therefore, I'd suggest making sure of 2 things:
emit
isn't being called on the widget prior to startup
onLoad
methodI suppose one final thing I should point out is that you shouldn't make a class inherit both _WidgetBase
and Evented
, as this may cause unintended side effects in widget logic, especially if you're extending an existing Dijit widget. Extending _WidgetBase
alone should suffice since it already defines its own emit
and on
(linked above).
Upvotes: 1