Select to view content in your preferred language

Dojo Widget Scoping

3049
2
Jump to solution
02-26-2015 08:06 AM
JustinShepard
Occasional Contributor II

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);
     ...
}
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

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));

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

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));

JustinShepard
Occasional Contributor II

Great. I'll start testing that today and let you know how it goes. Thanks for the quick response too!

0 Kudos