Select to view content in your preferred language

Unify two functions in just one to be called by a button

773
6
Jump to solution
02-09-2017 05:51 AM
GilbertoMatos
Occasional Contributor II

Hello!

I think this is a basic mistake I'm making, but I can not understand why.

I need to unify two functions that are called on two different buttons (first the _createPoint function and then the _search function) on just one button. The idea is simple. I created a new function, called _createBuffer, and inside it, I called in sequence to _createPoint and _search.

The problem is that when run by this unified function that I created, the line "var currentGraphic = this.graphicClickBufferLayer.graphics [0];" Of the _search function always returns "undefined", causing error in the rest of the code.

Does this occur because the graphical layer is still not actually on the map? I'm attaching the code for the entire widget.

Thanks for any help.

Gilberto.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Gilberto,

  Because your code looks like this:

_createBuffer: function()
{
  this._createPoint();
  this._search();
},‍‍‍‍‍

Both functions get called one right after the other and _createPoint is not complete before _search is called.

You need to wait for _createPoint to complete then call _search. You can do this a couple of ways. The fastest way would be to call the _search from the end of the _createPoint. The other way is to make the _createPoint return a deferred and then use:

_createBuffer: function()
{
  this._createPoint().then(function(){
    this._search();
  });
},‍‍‍‍‍‍‍‍‍‍

There are dozens of examples of this in other otb and custom widgets.

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

Gilberto,

  Because your code looks like this:

_createBuffer: function()
{
  this._createPoint();
  this._search();
},‍‍‍‍‍

Both functions get called one right after the other and _createPoint is not complete before _search is called.

You need to wait for _createPoint to complete then call _search. You can do this a couple of ways. The fastest way would be to call the _search from the end of the _createPoint. The other way is to make the _createPoint return a deferred and then use:

_createBuffer: function()
{
  this._createPoint().then(function(){
    this._search();
  });
},‍‍‍‍‍‍‍‍‍‍

There are dozens of examples of this in other otb and custom widgets.

0 Kudos
GilbertoMatos
Occasional Contributor II

Hello!

Robert, I tried to change my _createPoint function, following your recommendations. I checked the help dojo / deferred, but still I did not succeed. Below I leave my role, with the changes I made. I guess I could not figure it out how to make it return a deferred form correctly.

Thanks for the help.
Gilberto.

0 Kudos
GilbertoMatos
Occasional Contributor II

_createPoint:function() { var def = new Deferred(); // read input text var latitude = this.txtLatitude.value; var longitude = this.txtLongitude.value; var inSR = new esri.SpatialReference ( { wkid: this.defaultWkid } ); // create point var point = new Point(longitude,latitude,inSR); // draw point this._drawPoint(point, true); // create a buffer from point this._buffer(this, point); def.resolve("success"); return def.promise; }

0 Kudos
GilbertoMatos
Occasional Contributor II

Sorry, pasting the source code, the indentation and formatting were wrong.

Thank you!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Gilberto,

   You need more deferreds in your code. In your _createPoint function you call two other functions _drawPoint and _buffer. Draw point may be fine but _buffer uses the geometry service to buffer and it returns a promise and that takes time to complete. If you have a function that depends on the results of other functions they call, then you have to use deferreds.

0 Kudos
GilbertoMatos
Occasional Contributor II

Robert

I understand perfectly. I also found a contour solution, until I perform a more complete treatment for all functions. It worked by modifying my _createBuffer function to the one below. It's not a very reliable way, but it worked out right now.

Thanks again for the help.
Gilberto.

_createBuffer: function()
{
          var vm = this;
                                        
          this._createPoint().then
          (
              function(value)
              {
                  setTimeout
                  (
                      function()
                      {
                          vm._search();
                      },
                      2000
                  );
              }
          );
}

0 Kudos