Select to view content in your preferred language

How can I use a variable that is set in a function on an on event outside of the event?

4355
10
09-02-2015 04:57 AM
MichaelStranovsky
Regular Contributor

For instance:

featureLayer.on("selection-complete", function (result){

        var pPoly = result.features[0].geometry;

});

console.log(pPoly);

the console returns undefined

0 Kudos
10 Replies
RobertScheitlin__GISP
MVP Emeritus

Michael,

  That when you use lang.hitch to maintain the scope of the function:

Be sure to require dojo/_base/lang

featureLayer.on("selection-complete", lang.hitch(this,function (result){
        var pPoly = result.features[0].geometry;
        //Now to will have access to the external var inside this function
}));
console.log(pPoly);
0 Kudos
MichaelStranovsky
Regular Contributor

Hi Robert,

Thanks for the quick response but I actually want to access the pPoly variable outside the onEvent after the function has been executed.

Michael

0 Kudos
TimWitt2
MVP Alum

You can define the variable outside your function , but just so you know the console.log will still return undefined since it will run before your selection. To test you would have to put the console.log into your other "on" function.

var pPoly;
featureLayer.on("selection-complete", function (result){
        pPoly = result.features[0].geometry;
});
console.log(pPoly);
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Michael,

  Well think of it the same way now that you have access to the outside scope of the function you just set an outside var that you create to the pPoly.

var pPoly;
featureLayer.on("selection-complete", lang.hitch(this,function (result){  
        pPoly = result.features[0].geometry;  
        //Now to will have access to the external var inside this function  
}));  
console.log(pPoly); 
MichaelStranovsky
Regular Contributor

Yeah I tried that and the console still reports undefined but if I place the console.log inside the event it return correctly. I need to use it though outside the event.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Michael,

   And you are sure you use the code I had in my last post with the lang.hitch and the var outside the function?

0 Kudos
MichaelStranovsky
Regular Contributor

This is what I used:

var pPoly;

featureLayer.on("selection-complete", lang.hitch(this,function (result){

pPoly = result.features[0].geometry;

}));

console.log(pPoly);

0 Kudos
KenBuja
MVP Esteemed Contributor

The question deals more with the progression of the code. Even though the line "console.log" is after the function section, it will get called first. The code inside that function will only occur when that event takes place (i.e. after the selection is complete on the feature layer).

thejuskambi
Frequent Contributor

I think I understand why Micheal is having problem, the console is executed before the event is raised. The selectFeatures method is a deffered method. so you need to you something like this.

var pPoly;

featureLayer.on("selection-complete", lang.hitch(this,function (result){
     pPoly = result.features[0].geometry;
})).then(function(){
     console.log(pPoly);     
});