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?
Solved! Go to Solution.
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.
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); } });
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.
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); } });
Hmmmm...now the console shows:
error:undefinedlayer:[object Object]
And the alert box does not pop up at all.
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); } }); });
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.
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
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"?
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.