var MySumClass = declare(['dojo/Evented'], { constructor: function(x, y) { this.x = x; this.y = y; } dispatch: function() { this.emit('myEvent', { message: 'This is a test message.', result: this.x + this.y }); } }); var myInstance = new MySumClass(5,10); myInstance.on('myEvent', function(results) { console.log('message', results.message); console.log('results', results.result); }); myInstance.dispatch(); // console logs print at this point
JavaScript and Actionscript are quite similar in regards of syntax, especially if you ever did some AS2 development.
You can have an element dispatch an event similar to AS3
https://developer.mozilla.org/en-US/docs/DOM/EventTarget.dispatchEvent
Doing the same on regular JS objects (non-dom elements) takes some elbow grease, but Dojo in the JS API provides some very nice utilities to accomplish this.
http://livedocs.dojotoolkit.org/dojo/Evented
If you extend a new class based on dojo/Evented, you get access to some nice event delegation.
The Dojo docs seem to be acting up, but an example would be something likevar MySumClass = declare(['dojo/Evented'], { constructor: function(x, y) { this.x = x; this.y = y; } dispatch: function() { this.emit('myEvent', { message: 'This is a test message.', result: this.x + this.y }); } }); var myInstance = new MySumClass(5,10); myInstance.on('myEvent', function(results) { console.log('message', results.message); console.log('results', results.result); }); myInstance.dispatch(); // console logs print at this point
This also gives you the ability to to use dojo/on with dojo/Evented
http://livedocs.dojotoolkit.org/dojo/on
This opens up some cool uses where you can listen for events only once, pause them, remove them and so on.
JavaScript and Actionscript are quite similar in regards of syntax, especially if you ever did some AS2 development.
You can have an element dispatch an event similar to AS3
https://developer.mozilla.org/en-US/docs/DOM/EventTarget.dispatchEvent
Doing the same on regular JS objects (non-dom elements) takes some elbow grease, but Dojo in the JS API provides some very nice utilities to accomplish this.
http://livedocs.dojotoolkit.org/dojo/Evented
If you extend a new class based on dojo/Evented, you get access to some nice event delegation.
The Dojo docs seem to be acting up, but an example would be something likevar MySumClass = declare(['dojo/Evented'], { constructor: function(x, y) { this.x = x; this.y = y; } dispatch: function() { this.emit('myEvent', { message: 'This is a test message.', result: this.x + this.y }); } }); var myInstance = new MySumClass(5,10); myInstance.on('myEvent', function(results) { console.log('message', results.message); console.log('results', results.result); }); myInstance.dispatch(); // console logs print at this point
This also gives you the ability to to use dojo/on with dojo/Evented
http://livedocs.dojotoolkit.org/dojo/on
This opens up some cool uses where you can listen for events only once, pause them, remove them and so on.