Calling a function after using esri.request isn't working..

4376
4
Jump to solution
07-20-2015 12:42 PM
RahmathUnissa
New Contributor II

Hello,

I have created a function in a customized widget named as "Requestdata" where it requests the data from the soe url and returns a response as shown below.

Requestdata :function(){

  var LL = "Soe url";

  var list = esriRequest({

              url: LL,

              content: { f: "json" },

              handleAs: "json",

             callbackParamName: "callback",

             timeOut:0,

             },{useProxy:true});

   list.then(

function(response) {        

var features = response;

this.Newfunction();------------------------------>(Having an issue in calling this function)

  

                 },

  function(error) {

  console.log("Error: ");

                    }

  );

When I call a function named as "this.Newfunction" in the response function, it does not work. Also, the widget executes all other functions in the widget and then makes the request at last even though i request it it at the starting point.

Any suggestions on this??

Thanks &Regards,

Rahmath Unissa

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Rahmath,

  You have a simple scope problem that many new JS programmers run into. The problem is that within the list.then function the value of "this" is not the same as it would be outside the function. You get around this my using dojo/_base/lang.

Requestdata :function(){
  var LL = "Soe url";
  var list = esriRequest(
    {
      url: LL,
      content: { f: "json" },
      handleAs: "json",
      callbackParamName: "callback",
      timeOut:0,
    },{useProxy:true});
  list.then(
    lang.hitch(this, function(response) {        
      var features = response;
      this.Newfunction();------------------------------>(Having an issue in calling this function)
    },
    function(error) {
      console.log("Error: ");
    })
  );

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Rahmath,

  You have a simple scope problem that many new JS programmers run into. The problem is that within the list.then function the value of "this" is not the same as it would be outside the function. You get around this my using dojo/_base/lang.

Requestdata :function(){
  var LL = "Soe url";
  var list = esriRequest(
    {
      url: LL,
      content: { f: "json" },
      handleAs: "json",
      callbackParamName: "callback",
      timeOut:0,
    },{useProxy:true});
  list.then(
    lang.hitch(this, function(response) {        
      var features = response;
      this.Newfunction();------------------------------>(Having an issue in calling this function)
    },
    function(error) {
      console.log("Error: ");
    })
  );
RahmathUnissa
New Contributor II

Thank you Robert  that worked

0 Kudos
IanBridger
New Contributor II

Hi

This answer is the closest I've come to fixing my issue, but it's not working for me, I hope someone can help please.

I am trying to load a shapefile into a JS 3.25 app, and I've adapted the only example that seems to work on the internet (Add shapefile | ArcGIS API for JavaScript 3.25 ).

However, that example relies on the functions being siblings in a <script> tag in an html page, because when you're within the "request" object/block, "this" refers to the window, not the JS block/function/object.

But I've got to put the code into a widget that opens a file dialog and (within that dialog's event handler) reads the shapefiule and accepts/rejects the features based on the number of layers, etc. So in my case the functions I need to call are siblings to the event handler, not in a <script>tag at the window level.

I've managed to call an inner function to the event handler, and from there leapfrog to a sibling function, but it still has the window as scope, not the JS class/file/object.


var layer = theLayerINeedToUpdate;

addShapefileToMap: function ()
{
//the function I need to hit, and which needs to access "layer"
},


_shapefile: function() {
//please assume I've set up all variables correctly, that much is ok

function foo(){} // accessible

var pr = request({
url: window.um.state.generateUrl,
content: myContent,
form: document.getElementById('uploadForm'),
handleAs: 'json'
});
pr.then(
function (response) {

//foo() is accessible here, but can't reach layer...
addShapefileToMap(); //this can't reach the outer-sibling function AND mantain scope to reach "layer"
}, function(error){
//error handler

}
);
},

I've already tried a lang.hitch inside the then block:

var layer = theLayerINeedToHit;

addShapefileToMap: function ()
{
//the function I need to hit, and which needs to access "layer"
},


_shapefile: function() {
//please assume I've set up all variables correctly, that much is ok

var pr = request({
url: window.um.state.generateUrl,
content: myContent,
form: document.getElementById('uploadForm'),
handleAs: 'json'
});
pr.then(lang.hitch(this,
function (response) {
addShapefileToMap();
}, function(error){
//error handler

}
));
},

Even if I can call the function, that function's runtimje scope doesn't allow me to access the layer.

Thanks!!

Ian

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ian,

   Have you seen this widget?

https://community.esri.com/docs/DOC-6907 

0 Kudos