Call a function within queryTask showResults

3226
2
Jump to solution
05-20-2016 08:05 AM
Alexwang
Occasional Contributor II

Hi, I am very new to the WAB and dojo stuff. I really had a hard time with my first custom widget project. Please see below for a snippet of the codes. When I test the widget, an error appeared "TypeError: this.somethingElse() is not a function" within the _showResults. So how to make a call within _showResults and pass parameters to it? Thanks for your help!

_query: function(){

   var query = new Query();

   query.where = "Id > 0";

   query.outFields = ["ID", "Name"];

   query.returnGeometry = true;

   var queryTask = new QueryTask('http://.....');

   queryTask.on("complete", this._queryTaskExecuteCompleteHandler);

   queryTask.on("error", this._queryTaskErrorHandler);

   queryTask.execute(query, this._showResults);

},

_showResults: function(featureSet){

//do something here...

   this._somethingElse(featureSet.features);

},

_somethingElse: function(features){

     console.log(features.length);

     //and do something else here

}

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

  This is a code scope issue and you need to use and understand dojos lang.hitch.

_query: function(){

   var query = new Query();

   query.where = "Id > 0";

   query.outFields = ["ID", "Name"];

   query.returnGeometry = true;

   var queryTask = new QueryTask('http://.....');

   queryTask.on("complete", this._queryTaskExecuteCompleteHandler);

   queryTask.on("error", this._queryTaskErrorHandler);

   queryTask.execute(query, lang.hitch(this, this._showResults));

},

So what is going on here is that "this" is a different scope when you call _showResults and because "this" is a different scope that means that is can not find the this.somethingelse function. When you use lang.hitch it preserves the scope of "this" so that it can find this.something else. Be sure to add the require for lang to your widget ("dojo/_base/lang").

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Alex,

  This is a code scope issue and you need to use and understand dojos lang.hitch.

_query: function(){

   var query = new Query();

   query.where = "Id > 0";

   query.outFields = ["ID", "Name"];

   query.returnGeometry = true;

   var queryTask = new QueryTask('http://.....');

   queryTask.on("complete", this._queryTaskExecuteCompleteHandler);

   queryTask.on("error", this._queryTaskErrorHandler);

   queryTask.execute(query, lang.hitch(this, this._showResults));

},

So what is going on here is that "this" is a different scope when you call _showResults and because "this" is a different scope that means that is can not find the this.somethingelse function. When you use lang.hitch it preserves the scope of "this" so that it can find this.something else. Be sure to add the require for lang to your widget ("dojo/_base/lang").

View solution in original post

Alexwang
Occasional Contributor II

Thank you so much! You saved my day!

0 Kudos