How to correctly reference widget in event handlers and callbacks?

2563
2
Jump to solution
09-30-2015 02:43 PM
AndrewTerwiel
Occasional Contributor II

Say I have an event handler in my custom widget that handles a map click and executes an identify task. Within the event handler function and also the callback function for the identify.execute function I would like to reference the widget. Using the "this" keyword doesn't give the widget, it gives the event object or the window  object. The code below shows how I'm doing this, but I think this is not the right way.

_handleMapClick: function (event) {

var identifyTask = new IdentifyTask(url);

                    var identifyParams = new IdentifyParameters();

                    var layerQuery = new Query();

                    identifyParams.tolerance = 3;

                    identifyParams.returnGeometry = true;

                    identifyParams.layerIds = [159];

                    identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_VISIBLE;

                    identifyParams.width = landIdWidget.map.width;

                    identifyParams.height = landIdWidget.map.height;

                    identifyParams.geometry = event.mapPoint;

                    identifyParams.mapExtent = landIdWidget.map.extent;

                    identifyTask.execute(identifyParams, function (response) {

                                layerQuery.where = "ID = " + parId;

                                deferred = landIdWidget.that._myFeatureLayer.selectFeatures(layerQuery,

                                    FeatureLayer.SELECTION_NEW, function (selected) {

                                        landIdWidget.that._showParcelInfo(selected);

                                    });

                            }

                        }

                    });

}

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Andrew,

   This is where if you look at existing esri widgets you will see the use of lang.hitch(this, function() { format all the time. The use of dojo lang hitch is for keeping the function in scope and then "this" will be the widget and not the event.

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Andrew,

   This is where if you look at existing esri widgets you will see the use of lang.hitch(this, function() { format all the time. The use of dojo lang hitch is for keeping the function in scope and then "this" will be the widget and not the event.

0 Kudos
AndrewTerwiel
Occasional Contributor II

Thanks, Robert. I'll check that out.

0 Kudos