Copy link to clipboard
Copied
Hello,
I am attempting to put a statement in my .mxml file to make a private function restart if a certain condition is met. I am not pro at coding in .mxml, but I believe I have a start at solving this issue. Below it the code for my function, up to the point that the code would restart, if the condition is true:
private function populateLayerDataDictionary():void
{
//the smallest and largest scales reasonably possible
var smallestScale:Number = map.lods ? map.lods[0].scale : 1000000000;
var largestScale:Number = map.lods ? map.lods[map.lods.length - 1].scale : 1;var data:Object;
var priority:int = 0;
var agsVersion:Number;
// The next line controls whether the proxy in the <httpproxy> tag in config.xml
// is used for each layer. If want this set it to true; otherwise set it to false.
// If there is no <httpproxy> tag in config.xml then the value of useProxy doesn't matter.
var useProxy:Boolean = true;
for each (var layer:Layer in map.layers)
{
trace(layer.id);
if (layer is ArcGISDynamicMapServiceLayer)
{
var dmsLayer:ArcGISDynamicMapServiceLayer = layer as ArcGISDynamicMapServiceLayer;
if (useProxy) dmsLayer.proxyURL = configData.proxyUrl;
agsVersion = dmsLayer.version;
if (isNaN(agsVersion))
{
// This happens if the map includes a service that is turned off or doesn't exist
mapServicesProcessed++;
continue;
}//make an object for this map service to hold the layer information
var dmsMapServiceObj:Object = {id: dmsLayer.id, layers: new Array(), priority: priority++, url: dmsLayer.url, token: dmsLayer.token, proxyUrl: dmsLayer.proxyURL};
//add the map service object to the array of map services
mapServicesArray.push(dmsMapServiceObj);
// set the max and min scales
var maxScale:Number = layer.maxScale;
var minScale:Number = layer.minScale;
var getScales:Boolean = (maxScale == 0 && minScale == 0);
if (getScales)
{
for each (var layerInfo:LayerInfo in dmsLayer.layerInfos)
{
if (layerInfo.maxScale > 0 && (maxScale == 0 || layerInfo.maxScale < maxScale)) maxScale = layerInfo.maxScale;
if (layerInfo.minScale > 0 && layerInfo.minScale > minScale) minScale = layerInfo.minScale;
}layer.maxScale = maxScale > 0 ? maxScale : largestScale;
layer.minScale = minScale > 0 ? minScale : smallestScale;
}data = {name: dmsLayer.name, url: dmsLayer.url, token: dmsLayer.token, rtDefExpr: "", proxyUrl: dmsLayer.proxyURL};
dmsLayer.getAllDetails(new AsyncResponder(onResult, onFault, data));
}
else if (layer is ArcGISTiledMapServiceLayer)
{
var tmsLayer:ArcGISTiledMapServiceLayer = layer as ArcGISTiledMapServiceLayer;
if (useProxy) tmsLayer.proxyURL = configData.proxyUrl;
agsVersion = tmsLayer.version;
if (isNaN(agsVersion))
{
// This happens if the map includes a service that is turned off or doesn't exist
mapServicesProcessed++;
continue;
}//make an object for this map service to hold the layer information
var tmsMapServiceObj:Object = {id: tmsLayer.id, layers: new Array(), priority: priority++, url: tmsLayer.url, token: tmsLayer.token, proxyUrl: tmsLayer.proxyURL};
//add the map service object to the array of map services
mapServicesArray.push(tmsMapServiceObj);
if (layer.maxScale == 0 && layer.minScale == 0 && tmsLayer.tileInfo)
{
layer.maxScale = tmsLayer.tileInfo.lods[tmsLayer.tileInfo.lods.length - 1].scale;
layer.minScale = tmsLayer.tileInfo.lods[0].scale;
}data = {name: tmsLayer.name, url: tmsLayer.url, token: tmsLayer.token, rtDefExpr: "", proxyUrl: tmsLayer.proxyURL};
tmsLayer.getAllDetails(new AsyncResponder(onResult, onFault, data));
}
else if (layer is FeatureLayer)
{
var feaLayer:FeatureLayer = layer as FeatureLayer;
var layerDetails:LayerDetails = feaLayer.layerDetails;
var tableDetails:TableDetails = feaLayer.tableDetails;
if (layerDetails)
agsVersion = layerDetails.version;
else if (tableDetails)
agsVersion = tableDetails.version;
else
agsVersion = NaN;
if (isNaN(agsVersion))
{
// This happens if the map includes a service that is turned off or doesn't exist
mapServicesProcessed++;
continue;
}var url:String = feaLayer.url;
var pos:int = url.lastIndexOf("/");
url = url.slice(0, pos);//make an object for this map service to hold the layer information
var feaMapServiceObj:Object = {id: feaLayer.id, layers: new Array(), priority: priority++, url: url, token: feaLayer.token, proxyUrl: feaLayer.proxyURL};
//add the map service object to the array of map services
mapServicesArray.push(feaMapServiceObj);
data = {name: feaLayer.name, url: url, token: feaLayer.token, rtDefExpr: feaLayer.definitionExpression, proxyUrl: feaLayer.proxyURL};
var allDetails:AllDetails = new AllDetails();
allDetails.layersDetails = [layerDetails];
allDetails.tablesDetails = [tableDetails];
onResult(allDetails, data);
}
else
{
// skip other kinds of layers (e.g. image services)
mapServicesProcessed++;
}
// restarts and repeats populateLayerDataDictionary if there are missing services
if (mapServicesArray.length < 13)
{
init (private function populateLayerDataDictionary);
}
}
The last five lines are devoted to making the function restart itself if the condition 'mapServicesArray.length < 13'. The line 'init (private function populateLayerDataDictionary);' is supposed to trigger the restart, but I am not sure how to do it (this code clearly doesn't, it is a test of mine). Any advice would be welcome.
Thank you in advance! (and sorry for my poor code formatting in this post!)
Copy link to clipboard
Copied
Here is the code to the function as I have it so far:
private function populateLayerDataDictionary():void
{
//the smallest and largest scales reasonably possible
var smallestScale:Number = map.lods ? map.lods[0].scale : 1000000000;
var largestScale:Number = map.lods ? map.lods[map.lods.length - 1].scale : 1;
var data:Object;
var priority:int = 0;
var agsVersion:Number;
// The next line controls whether the proxy in the <httpproxy> tag in config.xml
// is used for each layer. If want this set it to true; otherwise set it to false.
// If there is no <httpproxy> tag in config.xml then the value of useProxy doesn't matter.
var useProxy:Boolean = true;
for each (var layer:Layer in map.layers)
{
trace(layer.id);
if (layer is ArcGISDynamicMapServiceLayer)
{
var dmsLayer:ArcGISDynamicMapServiceLayer = layer as ArcGISDynamicMapServiceLayer;
if (useProxy) dmsLayer.proxyURL = configData.proxyUrl;
agsVersion = dmsLayer.version;
if (isNaN(agsVersion))
{
// This happens if the map includes a service that is turned off or doesn't exist
mapServicesProcessed++;
continue;
}
//make an object for this map service to hold the layer information
var dmsMapServiceObj:Object = {id: dmsLayer.id, layers: new Array(), priority: priority++, url: dmsLayer.url, token: dmsLayer.token, proxyUrl: dmsLayer.proxyURL};
//add the map service object to the array of map services
mapServicesArray.push(dmsMapServiceObj);
// set the max and min scales
var maxScale:Number = layer.maxScale;
var minScale:Number = layer.minScale;
var getScales:Boolean = (maxScale == 0 && minScale == 0);
if (getScales)
{
for each (var layerInfo:LayerInfo in dmsLayer.layerInfos)
{
if (layerInfo.maxScale > 0 && (maxScale == 0 || layerInfo.maxScale < maxScale)) maxScale = layerInfo.maxScale;
if (layerInfo.minScale > 0 && layerInfo.minScale > minScale) minScale = layerInfo.minScale;
}
layer.maxScale = maxScale > 0 ? maxScale : largestScale;
layer.minScale = minScale > 0 ? minScale : smallestScale;
}
data = {name: dmsLayer.name, url: dmsLayer.url, token: dmsLayer.token, rtDefExpr: "", proxyUrl: dmsLayer.proxyURL};
dmsLayer.getAllDetails(new AsyncResponder(onResult, onFault, data));
}
else if (layer is ArcGISTiledMapServiceLayer)
{
var tmsLayer:ArcGISTiledMapServiceLayer = layer as ArcGISTiledMapServiceLayer;
if (useProxy) tmsLayer.proxyURL = configData.proxyUrl;
agsVersion = tmsLayer.version;
if (isNaN(agsVersion))
{
// This happens if the map includes a service that is turned off or doesn't exist
mapServicesProcessed++;
continue;
}
//make an object for this map service to hold the layer information
var tmsMapServiceObj:Object = {id: tmsLayer.id, layers: new Array(), priority: priority++, url: tmsLayer.url, token: tmsLayer.token, proxyUrl: tmsLayer.proxyURL};
//add the map service object to the array of map services
mapServicesArray.push(tmsMapServiceObj);
if (layer.maxScale == 0 && layer.minScale == 0 && tmsLayer.tileInfo)
{
layer.maxScale = tmsLayer.tileInfo.lods[tmsLayer.tileInfo.lods.length - 1].scale;
layer.minScale = tmsLayer.tileInfo.lods[0].scale;
}
data = {name: tmsLayer.name, url: tmsLayer.url, token: tmsLayer.token, rtDefExpr: "", proxyUrl: tmsLayer.proxyURL};
tmsLayer.getAllDetails(new AsyncResponder(onResult, onFault, data));
}
else if (layer is FeatureLayer)
{
var feaLayer:FeatureLayer = layer as FeatureLayer;
var layerDetails:LayerDetails = feaLayer.layerDetails;
var tableDetails:TableDetails = feaLayer.tableDetails;
if (layerDetails)
agsVersion = layerDetails.version;
else if (tableDetails)
agsVersion = tableDetails.version;
else
agsVersion = NaN;
if (isNaN(agsVersion))
{
// This happens if the map includes a service that is turned off or doesn't exist
mapServicesProcessed++;
continue;
}
var url:String = feaLayer.url;
var pos:int = url.lastIndexOf("/");
url = url.slice(0, pos);
//make an object for this map service to hold the layer information
var feaMapServiceObj:Object = {id: feaLayer.id, layers: new Array(), priority: priority++, url: url, token: feaLayer.token, proxyUrl: feaLayer.proxyURL};
//add the map service object to the array of map services
mapServicesArray.push(feaMapServiceObj);
data = {name: feaLayer.name, url: url, token: feaLayer.token, rtDefExpr: feaLayer.definitionExpression, proxyUrl: feaLayer.proxyURL};
var allDetails:AllDetails = new AllDetails();
allDetails.layersDetails = [layerDetails];
allDetails.tablesDetails = [tableDetails];
onResult(allDetails, data);
}
else
{
// skip other kinds of layers (e.g. image services)
mapServicesProcessed++;
}
// restarts and repeats populateLayerDataDictionary if there are missing services
if (mapServicesArray.length < 13)
{
init (private function populateLayerDataDictionary);
}
}