1

In the dojo Javascript library, dojo/on and dojo/aspect are used as functions that listen to events.

However I don't see how they differ from one another. Can someone explain when you would use on and when you would use aspect?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
IIS7 Rewrite
  • 779
  • 3
  • 16
  • 31

1 Answers1

6

dojo/on is used for listening to events. dojo/aspect is used to intercept calls to javascript functions.

With aspect, you can intercept a function call and do something before the function call, after, or both. With events, you are being notified that something occurred.

Technically, if the target object is not a domNode, dojo/on ends up calling aspect.after(...)

In <=1.6, there was not a distinction and dojo.connect was used. Functions were used to notify that an event occurred and there are still remnants of that in the code base. An example is using on with the click event on a dijit/Button.

dojo/Evented http://dojotoolkit.org/reference-guide/1.9/dojo/Evented.html

Craig Swing
  • 8,152
  • 2
  • 30
  • 43
  • so, if I understand correctly, if I want to listen to a dom event I should use dojo/on, but if I want to hook up to a widget function call / event I should use dojo/aspect. Right? – IIS7 Rewrite May 09 '13 at 19:36
  • Close. Yes on the dom node. Yes on the function call. But there is a new mixin called Evented. Widgets can emit events (without the functions) using this.emit('eventName', {}); In the latter case, you should use dojo/on. – Craig Swing May 09 '13 at 19:43