AnsweredAssumed Answered

Dojo Widget Scoping

Question asked by shepa006 on Feb 26, 2015
Latest reply on Feb 26, 2015 by shepa006

I'm working on a reusable widget. I started with the HomeButton sample, Create a Re-usable Widget | Guide | ArcGIS API for JavaScript , and am now trying to make my own. I'm getting stuck in a scoping issue though. I want to allow the user to draw a graphic and then pass that graphic back to the widget so that other components of the widget can act on the the result. So like the demo shows I have the widget referenced using the this keyword which looks like it keeps the unique widget. I can get the drawing to activate fine and the handler grabs the geometry and makes a graphic as it should but once it is passed into the handler, drawingCompleteHandler, I can't get the graphic or anything back out of the handler and using 'this' once it's in the handler function will reference the draw instead of the widget. I was thinking that there might be a way to use a promise to get the result from the handler back out to the larger widget but I'm not too familiar with using promises. I could set a global variable but then if someone create multiple instances of the widget they all end using the same global; not to mention the standard downsides to using a global variable.



drawingCompleteHandler: function (evt) {
     // here 'this' refers to the draw instead of the widget so I can't interact with the widget anymore
     ...blah blah blah code to handle the evt result
     return graphic;
_init: function () {
     // here 'this' refers to the widget
     on(this.drawTools, "draw-complete", this.drawingCompleteHandler);