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 AS3https://developer.mozilla.org/en-US/docs/DOM/EventTarget.dispatchEventDoing 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/EventedIf 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/Eventedhttp://livedocs.dojotoolkit.org/dojo/onThis opens up some cool uses where you can listen for events only once, pause them, remove them and so on.
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
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.