connect to on (layer-add-result)

3657
10
Jump to solution
10-29-2015 01:56 PM
LauraMiles1
Occasional Contributor III

I'm trying to set up an alert box to fire if my layers do not load to let the user know which ones did not load. I am not sure why when I convert from some sample code I found using dojo.connect, it no longer works.

This works:

    var arrayErrors = [];
    dojo.connect(mapObj, "onLayerAddResult", function(error){
    if (error) {
        var errMess = error.message;
        arrayErrors.push(errMess);
        alert("Added to array\n" + errMess);
    }
});

Whereas this does not:

var arrayErrors = [];
on(mapObj, "layer-add-result", function(error){
    if (error) {
        var errMess = error.message;
        arrayErrors.push(errMess);
        alert("Added to array\n" + errMess);
    }
});

Using dojo.connect in the first code block, the alert box pops up with the error message for each service that does not load. In the second code block the alert box does not pop up at all. However if I put an alert box before the "if" block, that one will pop up - so for some reason an error is  not being logged? Does anyone see any problems in my code?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Laura,

   Here is your fiddle updated. updated - JSFiddle but the error that is returned from providing a bogus url like you are doing is not very helpful and is not a normal error object but a string.

View solution in original post

0 Kudos
10 Replies
RobertScheitlin__GISP
MVP Emeritus

Laura,

  The layer-add-result event return two objects the layer and the JS error object. I would add add layer return parameter to your result function.

var arrayErrors = []; 
on(mapObj, "layer-add-result", function(error, layer){
    console.info("error:", error, "layer:", layer);
    if (error) {
        var errMess = error.message;
        arrayErrors.push(errMess);
        alert("Added to array\n" + errMess);
    } 
});
LauraMiles1
Occasional Contributor III

Hi Robert,

This is progress, as the alert box after the "if" statement now pops up - so it seems an error is being triggered - however, it says that errMess is undefined and it's also popping up for all 9 services instead of just the two that are stopped.

In the console, I get:

error:[object Object]layer:undefined

for each of my 9 services.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Laura,

  So it sounds like the error and layer parameters need to be flipped in the result function.

var arrayErrors = [];
on(mapObj, "layer-add-result", function(layer ,error){
    console.info("error:", error, "layer:", layer);
    if (error) {
        var errMess = error.message;
        arrayErrors.push(errMess);
        alert("Added to array\n" + errMess);
    }
});
LauraMiles1
Occasional Contributor III

Hmmmm...now the console shows:

error:undefinedlayer:[object Object]

And the alert box does not pop up at all.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Laura,

  Hmm... Now I am testing and seeing the issue too. What i did get to work though is the maps addLayers function instead of addLayer.

    var arrayErrors = [];
      on(map, "layers-add-result", function(results){
        console.info("layers:", results.layers);
        array.map(results.layers, function(result){
          if(result.error){
            var errMess = result.error.message;
            arrayErrors.push(errMess);
            alert("Added to array\n" + errMess);
          }
        });
      });
LauraMiles1
Occasional Contributor III

Hi Robert,

I'm getting an error on array.map - do I need to define 'array' or add a reference?

I was using layer-add-result so that it would add the error for each service to arrayErrors so I could show them all at the end in one alert box (triggered by layers-add-result). But if this does the same (adds each service's error message to the array) that's great.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Laura,

   Yes you need a require and var for array "dojo/_base/array".

Yes it has an array of layers added along with the error if one is present

LauraMiles1
Occasional Contributor III

Hi Robert,

I did have that reference already but it's still not working. I've created a jsfiddle - f12 tells me "array is not defined"?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Laura,

   Here is your fiddle updated. updated - JSFiddle but the error that is returned from providing a bogus url like you are doing is not very helpful and is not a normal error object but a string.

0 Kudos