How to clear Geocoder results from a different widget

3894
5
Jump to solution
02-25-2015 02:47 PM
DouglasGuess
Occasional Contributor

Within the Geocoder Widget.js file, _initGeocoder function, if I add the following line:

this.own(on(geocoder, "clear", lang.hitch(this, "clearResults")));

and then add the corresponding "clearResults" function, I am able to clear the map of any graphic selection and/or popup.  However, I want to accomplish this task within another widget and not modify the original geocoder widget.  Within my other widget, I know I need to use aspect but not quite sure if I should use .before or .after.  I think .before makes more sense, but I'm not clear how to incorporate the line of code above into the _initGeocoder function and then call the clearResults function from within my widget.  This seems like an easy task but I can't seem to get the syntax right.  Any help/thoughts would be great!

Thanks

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Douglas,

  The way I would do it is use dojo query to get the widgets dom not and then use registry to get the dijit from the dom node.

Here is code that you can run from another widget to get to the geocoder:

     var domId = query('.jimu-widget-geocoder');
     var geocodeId = domAttr.get(domId[0], 'id');
     var geocoderWidget = registry.byId(geocodeId);
     //console.info(geocoderWidget);
     var geocoder = registry.byId(domAttr.get(geocoderWidget.domNode.children[0],'id'));
     //console.info(geocoder);
     on(geocoder, 'clear', lang.hitch(this, 'clearResults'));

Of course if you have your geocoder widget configured to use more than one geocode service then you will need to grab the next child.

var geocoder2 = registry.byId(domAttr.get(geocoderWidget.domNode.children[1],'id'));

View solution in original post

5 Replies
LarryStout
Occasional Contributor III

Doug,

The problem with this approach is that the Geocoder Widget starts automatically and you can't get access to the _initGeocoder() function before it has run.  I noticed the findComplete function runs with every find, so you could do it this way:

aspect.after(widget, 'findComplete', lang.hitch(this, function(response) {
  if (!this.geocoderAltered) {
    this.geocoderAltered = true;
    widget.own(on(response.target, 'clear', lang.hitch(this, 'clearResults')));
  }
}), true);

I've tested this and it seems to work,

Larry

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Douglas,

  The way I would do it is use dojo query to get the widgets dom not and then use registry to get the dijit from the dom node.

Here is code that you can run from another widget to get to the geocoder:

     var domId = query('.jimu-widget-geocoder');
     var geocodeId = domAttr.get(domId[0], 'id');
     var geocoderWidget = registry.byId(geocodeId);
     //console.info(geocoderWidget);
     var geocoder = registry.byId(domAttr.get(geocoderWidget.domNode.children[0],'id'));
     //console.info(geocoder);
     on(geocoder, 'clear', lang.hitch(this, 'clearResults'));

Of course if you have your geocoder widget configured to use more than one geocode service then you will need to grab the next child.

var geocoder2 = registry.byId(domAttr.get(geocoderWidget.domNode.children[1],'id'));
DouglasGuess
Occasional Contributor

Thanks Robert and Larry for your input.  I tested both methods and they both work, but decided to use Robert's.  It seemed like it was a cleaner approach to access the geocoder widget.  Thanks again guys!

0 Kudos
LarryStout
Occasional Contributor III

I like Robert's approach better also.  I was curious about using multiple geocoders, and did some testing.  It seems the active geocoder is always geocoderWidget.domNode.children[0], and there never is a geocoderWidget.domNode.children[1].  Furthermore, the event listener assigned to geocoderWidget.domNode.children[0] seems to transfer to any active geocoder.  Strange.

Here's a variant on Robert's approach that I tested with two geocoders:

query('.jimu-widget-geocoder > .simpleGeocoder').forEach(lang.hitch(this, function(node) {
  var geocoder = registry.byId(node.id);
  on(geocoder, 'clear', lang.hitch(this, 'clearResults'));
}));

The forEach() construct implies multiple objects will be returned, but in fact, only one is ever returned.

Larry

RobertScheitlin__GISP
MVP Emeritus

Larry,

   Nice, I knew the code could be optimized.

0 Kudos