OK - I've a site I wrote years back which did work, but now due to a data centre move I realise it onl;y worked becasue the requests were resolved quickly, on the "new" network my code needs attention for timing.
I have this
dojo.connect(dynamicMapServiceLayer, "onLoad", function () {
var content = "";
var i = 0
//return the layer descriptions and © text into arrays - we use this later for the info dialog to generate hyperlinks and © text
dojo.forEach(dynamicMapServiceLayer.layerInfos, function (layer) {
//console.debug(dynamicMapServiceLayer.layerInfos[layer.id])
var requestHandle = esri.request({
url: mapserviceCustom + "/" + layer.id + "?f=json",
handleAs: "json",
load: function (data) {
LayerAttach[layer.id] = data.hasAttachments;
LayerDescriptions[layer.id] = data.description;
//LayerCopyright[layer.id] = data.copyrightText;
console.debug(dynamicMapServiceLayer)
dynamicMapServiceLayer.layerInfos[layer.id].copyrightText = data.copyrightText;
},
}, { useProxy: true });
});
});
That runs during the init of my site and gets more info from the REST of the service regarding each layer, I use this to dynamically control the copyright notice on the map, as this varies per layer. I store this info in the layer description.
I've not looked at this for a long time, but I know I need to do something to stop the rest of the code working until all this has run, as a later part of my init code will use the results - but my hack skills are exhausted. Most of the site is in legacy and most examples I have found are in AMD. So any help gratefully received.
Cheers
ACM
Solved! Go to Solution.
Adrian,
I am not real sure about legacy code but in AMD I would use a Promise/All (which you likely can use in your legacy code). The way it would work is:
dojo.connect(dynamicMapServiceLayer, "onLoad", function () {
var content = "";
var i = 0;
//return the layer descriptions and © text into arrays - we use this later for the info dialog to generate hyperlinks and © text
var defs = dojo.map(dynamicMapServiceLayer.layerInfos, dojo._base.lang.hitch(this, function (layer) {
return esri.request({
url: mapserviceCustom + "/" + layer.id + "?f=json",
content: {
f: 'json'
},
handleAs: 'json',
callbackParamName: 'callback'
});
}));
dojo.promise.all(defs).then(function (results) {
//now you are ready to proceed
dojo.forEach(results, function(data){
LayerAttach[data.id] = data.hasAttachments;
LayerDescriptions[data.id] = data.description;
console.debug(dynamicMapServiceLayer)
dynamicMapServiceLayer.layerInfos[data.id].copyrightText = data.copyrightText;
});
});
});
Adrian,
I am not real sure about legacy code but in AMD I would use a Promise/All (which you likely can use in your legacy code). The way it would work is:
dojo.connect(dynamicMapServiceLayer, "onLoad", function () {
var content = "";
var i = 0;
//return the layer descriptions and © text into arrays - we use this later for the info dialog to generate hyperlinks and © text
var defs = dojo.map(dynamicMapServiceLayer.layerInfos, dojo._base.lang.hitch(this, function (layer) {
return esri.request({
url: mapserviceCustom + "/" + layer.id + "?f=json",
content: {
f: 'json'
},
handleAs: 'json',
callbackParamName: 'callback'
});
}));
dojo.promise.all(defs).then(function (results) {
//now you are ready to proceed
dojo.forEach(results, function(data){
LayerAttach[data.id] = data.hasAttachments;
LayerDescriptions[data.id] = data.description;
console.debug(dynamicMapServiceLayer)
dynamicMapServiceLayer.layerInfos[data.id].copyrightText = data.copyrightText;
});
});
});
Many thanks - Embarrassingly, when I was searching for an answer, I found I'd already asked the same question in March!
Anyway, I'll give that a go when I have time and get back. For now I put a awful hack in the part that calls this info not to fire off until the array length of LayerDescriptions equals the number of layers
I gave it a very quick go - a slight modification, not "dojo._base.lang.hitch" but "dojo.hitch" - that gets it "working" - it is giving me loads of proxy errors now - but I'll have to look at them a different day
Many thanks
Mark as the answer - many thank - I think I need a bit more tweaking in my next bit of code, but I hink the wait is there now.
Cheers