Call back return value Scope problem

4334
6
03-28-2016 02:47 AM
NadirHussain
Occasional Contributor II

var dist1;

getDistance(clickedPt,gp.geometry,function(dist){

                              dist1=dist;

                              alert("inside"+dist1);

                           

                         });

alert("outside"+dist1);

result inside block shows.but outside undefined.how to get dist outside from get distance block.dist1 undefined.Please help what can be the reason.

Thanks

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

Nadir,

  You use dojo lang.hitch to keep your function in scope:

var dist1;
getDistance(clickedPt, gp.geometry, lang.hitch(this, function(dist){
  dist1=dist;
  alert("inside"+dist1);
}));
alert("outside"+dist1);
NadirHussain
Occasional Contributor II

Dear Robert,

I call my function same like you explain above

statment

alert("outside"+dist1);not giving any result.

alert("inside"+dist1);giving result.

so i am still stuck for scope variable.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nadir,

I will have to see more of your code then as this should work.

0 Kudos
NadirHussain
Occasional Contributor II

Dear Robert,

My function defination:

function getDistance (p1,p2,callback) {

                       

                            var distParams = new esri.tasks.DistanceParameters(); 

                            distParams.distanceUnit = esri.tasks.GeometryService.UNIT_STATUTE_MILE;     

                            distParams.geometry1 = p1;   

                            distParams.geometry2 = p2;    

                            distParams.geodesic = true;

                            var gsvc=  new esri.tasks.GeometryService("http://myserver/arcgis/rest/services/Utilities/Geometry/GeometryServer");

                            gsvc.distance(distParams,function(response){

                                     callback(response);

                             });

                            

                        }

function calling:

                                        var dist1; 

                                    getDistance(clickedPt,gp.geometry,  dojo._base.lang.hitch(this, function(dist){

                                        dist1=dist;

                                        alert("inside"+dist1);

                                       }));

                                     alert("outside"+dist1);

outside nothing show.inside alert show values.i want this outside callback block.

Thanks.

0 Kudos
TyroneBiggums
Occasional Contributor III

I don't think you can do things like that. First, the lang.hitch maintains the context in your objects. I don't think context and scope are the same. I think hitch only keeps up with what "this" is.

Second, you're calling an asynchronous function (your getDistance) that calls an asynchronous function (geo svc function). Your outside alert is going to run synchronously and won't care about the asynchronous timing of the other objects.

Please see my code snippet below for how I show that dist1 remains in scope if you move things around. I have a map click to start the get distance. I have a second test button click event that alerts dist1 for the 'outside'.

var dist1;

var getDistanceCallback = function(x) {
    dist1 = x
    alert('inside callback ' + dist1);
}

map.on("click", function(evt) {
    var sReference = new SpatialReference(102100);
    var newGraphic = new Point(0, 0, sReference);
    var newGraphic2 = new Point(75, Math.floor((Math.random() * 50) + 1), sReference);
    
    getDistance(newGraphic, newGraphic2, getDistanceCallback);
});

on(dom.byId('btn'), 'click', function() {
    alert('button: ' + dist1);
});