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); ... }
Solved! Go to Solution.
Justin,
This is the exact situation where lang.hitch comes into play.
require: dojo/_base/lang
on(this.drawTools, "draw-complete", lang.hitch(this, this.drawingCompleteHandler));
Justin,
This is the exact situation where lang.hitch comes into play.
require: dojo/_base/lang
on(this.drawTools, "draw-complete", lang.hitch(this, this.drawingCompleteHandler));
Great. I'll start testing that today and let you know how it goes. Thanks for the quick response too!