scope issue, setting 'this' object for use later

401
5
Jump to solution
10-28-2020 04:28 PM
JamesCrandall
MVP Frequent Contributor

I'm falling short here and need some help.  I need to ensure that 'this.fsTok' gets set at the startup before 'this.createServerSiteScratchLayer()' but it seems that no matter what fsTok is always undefined in the createServerSiteScratchLayer() function.

fsTok: null

startup: function(){
	this.fsTok = this.getFStokFromUrl();
        this.createServerSideScratchLayer();

},

getFStokFromUrl: function () {
             
	tok = ""
	var gp = new Geoprocessor("gpServiceURL/execute");
	var params = { "inSystem": "someInputParamValue"};
	gp.execute(params, retTok);
	function retTok(results) {
		tok = results[0].value;
	}

	return tok
  
},

createServerSideScratchLayer: function () {              
  this.serverSideScratchLayer = new FeatureLayer(this.config.applicationFeatureLayer + '?token=' + this.fsTok,
	  {
		  outFields: ["OBJECTID"]
	  });              
},‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
1 Solution

Accepted Solutions
ManishPatel
Esri Contributor

Can you try with this instead 

createServerSideScratchLayer(tok)

You can try to debug the code and see if you are receiving the token and also if the function is approachable.

Or alternatively, you can move your code snippet in the response function instead.

getFStokFromUrl: function () {
             
     tok = ""
     var gp = new Geoprocessor("gpServiceURL/execute");
     var params = { "inSystem": "someInputParamValue"};
     gp.execute(params, retTok);
     function retTok(results) {
          tok = results[0].value;
          this.serverSideScratchLayer = new FeatureLayer(this.config.applicationFeatureLayer + '?token=' +
tok 
,
       {
            outFields: ["OBJECTID"]
       });     

     } 
},

Try this if that helps.

Cheers,

Manish

Cheers,
Manish

View solution in original post

5 Replies
ManishPatel
Esri Contributor

Hi James Crandall,

The code looks correct, however it depends if your gp service is sync or async and whether the function waits till the response is received back.

Ideally, you can check if the response is received and put the createServerSiteScratchLayer()  in the getFStokFromUrl function's response.

fsTok: null

startup: function(){
     this.fsTok = this.getFStokFromUrl();
        

},

getFStokFromUrl: function () {
             
     tok = ""
     var gp = new Geoprocessor("gpServiceURL/execute");
     var params = { "inSystem": "someInputParamValue"};
     gp.execute(params, retTok);
     function retTok(results) {
          tok = results[0].value;
          //this.fsTok = results[0].value; you can set this if you need to use it again
          this.createServerSideScratchLayer(tok);//or pass as parameter to the function
     }  
},

createServerSideScratchLayer: function (token) {              
  this.serverSideScratchLayer = new FeatureLayer(this.config.applicationFeatureLayer + '?token=' + token,
       {
            outFields: ["OBJECTID"]
       });              
},

Cheers,

Manish

 

If this answer solved your question or if you found it helpful please mark it accordingly to help others who have the same question.

Cheers,
Manish
JamesCrandall
MVP Frequent Contributor

Thanks Manish,

I followed your approach but getting "this.createServerSideScratchLayer is not a function" error on this line,

this.createServerSideScratchLayer(tok);//or pass as parameter to the function
0 Kudos
ManishPatel
Esri Contributor

Can you try with this instead 

createServerSideScratchLayer(tok)

You can try to debug the code and see if you are receiving the token and also if the function is approachable.

Or alternatively, you can move your code snippet in the response function instead.

getFStokFromUrl: function () {
             
     tok = ""
     var gp = new Geoprocessor("gpServiceURL/execute");
     var params = { "inSystem": "someInputParamValue"};
     gp.execute(params, retTok);
     function retTok(results) {
          tok = results[0].value;
          this.serverSideScratchLayer = new FeatureLayer(this.config.applicationFeatureLayer + '?token=' +
tok 
,
       {
            outFields: ["OBJECTID"]
       });     

     } 
},

Try this if that helps.

Cheers,

Manish

Cheers,
Manish
JamesCrandall
MVP Frequent Contributor

this works, thanks.  Now it has uncovered an additional scope issue when I try to access this.serverSideScracthLayer from another function.

It's very difficult to inherit code from another developer that has written so many functions that separate out all of the logic. Plus I'm still not fully understanding scope and accessing/setting variables that can be used later on.

ManishPatel
Esri Contributor

I am glad that worked out for you.

I understand when trying to inherit code from another developer it an always be challenging but I am sure you will find a way out :) 

Regarding the scope issue, I usually either pass it as a parameter from function to function or create a global class that holds all the variables and reference it throughout the application.

Cheers,
Manish