Layer objects cannot be accessed

2110
8
03-10-2011 02:18 AM
RichardKaufholz
New Contributor
Hello,

I am having trouble accessing the objects of a dynamiclayer using JSAPI v2.1. Its strange, because Im almost certain that I have done it before..

I have the following lines executing:
console.log("---- OBJECT DEBUG STARTS HERE ----");
console.log(layer);
console.log(layer.version);
console.log(layer.layerInfos);
console.log("---- OBJECT DEBUG END HERE ----");


Chrome console shows the following:
---- OBJECT DEBUG STARTS HERE ----
basemapviewer.html:118
a
_defaultVisibleLayers: Array[29]
_div: HTMLDivElement
_divAlphaImageFunc: function (){return _bb.apply(_ba,arguments||[]);}
_errorHandler: function (){return _bb.apply(_ba,arguments||[]);}
_exportMapImageHandler: function (){return _bb.apply(_ba,arguments||[]);}
_img: HTMLImageElement
_imgSrcFunc: function (){return _bb.apply(_ba,arguments||[]);}
_img_dragOrigin: Object
_img_loading: HTMLImageElement
_inherited: Object
_initLayer: function (){return _bb.apply(_ba,arguments||[]);}
_layerIndex: 2
_load: function (){return _bb.apply(_ba,arguments||[]);}
_loadCallback: undefined
_map: a
_onExtentChangeHandler_connect: Array[4]
_onPanHandler_connect: Array[4]
_onResizeHandler_connect: Array[4]
_onZoomHandler_connect: Array[4]
_opacityChangeHandler_connect: Array[4]
_params: Object
_setIsPNG32: function (){return _bb.apply(_ba,arguments||[]);}
_startRect: Object
_tileErrorHandler: a
_tileLoadHandler: a
_titleForLegend: "INFORMATION"
_unsetMap_connect: Array[4]
_updateStarted: true
_url: Object
_visibilityChangeHandler_connect: Array[4]
capabilities: "Map,Query,Data"
copyright: ""
description: ""
dpi: 96
fullExtent: a
getImageUrl: function (){return _bb.apply(_ba,arguments||[]);}
id: "layer2"
imageFormat: "png8"
imageTransparency: true
initialExtent: a
isPNG32: undefined
layerInfos: Array[29]
legendResponse: Object
loaded: true
onLoad: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
onOpacityChange: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
onUpdateEnd: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
onVisibilityChange: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
opacity: 0.75
ovcConnect: Array[4]
spatialReference: a
units: "esriDecimalDegrees"
url: "--removed--"
useMapImage: false
version: 10.01
visibleLayers: Array[29]
__proto__: anonymous
basemapviewer.html:119undefined
basemapviewer.html:120[]
basemapviewer.html:122---- OBJECT DEBUG END HERE ----


What you can see here is that the first console line prints the layer object ("a") - have expanded it to show all the objects stored within it (including "version" and "layerInfos"). However, when it is required to write one of these objects, it shows "undefined" and an empty array.

I have tried accessing by looping through the object as well, with the same results. I have tried calling the object using different JS notation, without success. I have also (out of desperation) re-published and restarted the service and tried on a different service all with the same results.

What is (clearly) incongruous here is that the full object can be read fine, but the embedded objects cannot. Presumably I am missing something OBVIOUS, but Ive tried re-writing a million times without success.

Please help!

Regards,
Richard.
0 Kudos
8 Replies
HemingZhu
Occasional Contributor III
Hello,

I am having trouble accessing the objects of a dynamiclayer using JSAPI v2.1. Its strange, because Im almost certain that I have done it before..

I have the following lines executing:
console.log("---- OBJECT DEBUG STARTS HERE ----");
console.log(layer);
console.log(layer.version);
console.log(layer.layerInfos);
console.log("---- OBJECT DEBUG END HERE ----");


Chrome console shows the following:
---- OBJECT DEBUG STARTS HERE ----
basemapviewer.html:118
a
_defaultVisibleLayers: Array[29]
_div: HTMLDivElement
_divAlphaImageFunc: function (){return _bb.apply(_ba,arguments||[]);}
_errorHandler: function (){return _bb.apply(_ba,arguments||[]);}
_exportMapImageHandler: function (){return _bb.apply(_ba,arguments||[]);}
_img: HTMLImageElement
_imgSrcFunc: function (){return _bb.apply(_ba,arguments||[]);}
_img_dragOrigin: Object
_img_loading: HTMLImageElement
_inherited: Object
_initLayer: function (){return _bb.apply(_ba,arguments||[]);}
_layerIndex: 2
_load: function (){return _bb.apply(_ba,arguments||[]);}
_loadCallback: undefined
_map: a
_onExtentChangeHandler_connect: Array[4]
_onPanHandler_connect: Array[4]
_onResizeHandler_connect: Array[4]
_onZoomHandler_connect: Array[4]
_opacityChangeHandler_connect: Array[4]
_params: Object
_setIsPNG32: function (){return _bb.apply(_ba,arguments||[]);}
_startRect: Object
_tileErrorHandler: a
_tileLoadHandler: a
_titleForLegend: "INFORMATION"
_unsetMap_connect: Array[4]
_updateStarted: true
_url: Object
_visibilityChangeHandler_connect: Array[4]
capabilities: "Map,Query,Data"
copyright: ""
description: ""
dpi: 96
fullExtent: a
getImageUrl: function (){return _bb.apply(_ba,arguments||[]);}
id: "layer2"
imageFormat: "png8"
imageTransparency: true
initialExtent: a
isPNG32: undefined
layerInfos: Array[29]
legendResponse: Object
loaded: true
onLoad: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
onOpacityChange: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
onUpdateEnd: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
onVisibilityChange: function (){var ap=Array.prototype,c=arguments.callee,ls=c._listeners,t=c.target;var r=t&&t.apply(this,arguments);var i,lls;lls=[].concat(ls);for(i in lls){if(!(i in ap)){lls.apply(this,arguments);}}return r;}
opacity: 0.75
ovcConnect: Array[4]
spatialReference: a
units: "esriDecimalDegrees"
url: "--removed--"
useMapImage: false
version: 10.01
visibleLayers: Array[29]
__proto__: anonymous
basemapviewer.html:119undefined
basemapviewer.html:120[]
basemapviewer.html:122---- OBJECT DEBUG END HERE ----


What you can see here is that the first console line prints the layer object ("a") - have expanded it to show all the objects stored within it (including "version" and "layerInfos"). However, when it is required to write one of these objects, it shows "undefined" and an empty array.

I have tried accessing by looping through the object as well, with the same results. I have tried calling the object using different JS notation, without success. I have also (out of desperation) re-published and restarted the service and tried on a different service all with the same results.

What is (clearly) incongruous here is that the full object can be read fine, but the embedded objects cannot. Presumably I am missing something OBVIOUS, but Ive tried re-writing a million times without success.

Please help!

Regards,
Richard.


I noticed this problem too. If i defined a dynamic layer in a method and than query some of its properties such as layerInfos rightway. i will get the similar error message. However, if i defined the layer in one method lile init(), and then query its properties in other method. It worked just fine. Strange is it ? Maybe it is a bug or maybe it is just dojo.Deferred. Would be interested to know...
0 Kudos
KellyHutchins
Esri Frequent Contributor
Here's a snippet that shows how to access the layer properties- in order to access all the information you'll need to wait until the layer is loaded.

dojo.connect(operationalLayer,"onLoad",function(){
    console.log(operationalLayer.version);
    console.log(operationalLayer.layerInfos);      
    console.log(operationalLayer.layerInfos[0].name);
});
0 Kudos
HemingZhu
Occasional Contributor III
Here's a snippet that shows how to access the layer properties- in order to access all the information you'll need to wait until the layer is loaded.

dojo.connect(operationalLayer,"onLoad",function(){
    console.log(operationalLayer.version);
    console.log(operationalLayer.layerInfos);      
    console.log(operationalLayer.layerInfos[0].name);
});


Well it certainly give an answer. However, there are cases where you want to access and/or change the properties such as layer visibility before adding the layer to the map. How do you address those cases?
0 Kudos
HemingZhu
Occasional Contributor III
Well it certainly give an answer. However, there are cases where you want to access and/or change the properties such as layer visibility before adding the layer to the map. How do you address those cases?


After carefully review the API. Kelly, you are right on this! Layer's onLoad event happens before adding the layer to the map. So you indeed can access or change the properties before adding the layer to the map.
0 Kudos
RichardKaufholz
New Contributor
Hi guys,

Sorry for the delay - have been solving other problems in the meantime 😉

Kelly - the method I use to call the quoted functions below is:
dojo.connect(coreData, "onLoad", buildLayerList(coreData));

where "coreData" is a dynamic layer object.

What is strange, is that the first line successfully prints the entire object to the console, but when I look for specific values in the next two lines, it prints out "undefined" and "[]". To me, that means that the data is loaded, the object can be read in its entirety, but I cannot access its individual components.

Any other ideas?

Thanks,
Richard.
0 Kudos
RichardKaufholz
New Contributor
Ok, here goes:

After literally playing around with pretty much every combination of code, in the blind hope that something would "appear" to me, I have discovered the following:

When passing the layer object via a parameter to my function (after the layer has loaded):
  - the object can be written in full to the console
  - specific object properties are empty/null/bank and cannot be written

However, when I run the exact same procedure, but call the layer object directly (i.e. no variables are passed as arguments to the function):
  - the object can be written in full to the console
  - any object property can be successfully read - including the layerInfos array

The function argument seems to be the culprit, because even if I reference the original layer object, when an arbitrary (unused) argument is shared to the function, it stops working again.

Now, Im not a professional developer, but that sure seems strange to me! Dont suppose anyone can throw some light on this conundrum?

Richard.
0 Kudos
HemingZhu
Occasional Contributor III
Ok, here goes:

After literally playing around with pretty much every combination of code, in the blind hope that something would "appear" to me, I have discovered the following:

When passing the layer object via a parameter to my function (after the layer has loaded):
  - the object can be written in full to the console
  - specific object properties are empty/null/bank and cannot be written

However, when I run the exact same procedure, but call the layer object directly (i.e. no variables are passed as arguments to the function):
  - the object can be written in full to the console
  - any object property can be successfully read - including the layerInfos array

The function argument seems to be the culprit, because even if I reference the original layer object, when an arbitrary (unused) argument is shared to the function, it stops working again.

Now, Im not a professional developer, but that sure seems strange to me! Dont suppose anyone can throw some light on this conundrum?

Richard.


I looked at your code statement: dojo.connect(coreData, "onLoad", buildLayerList(coreData)); buildLayerList(coreData) suppose to be a delegate meaning the name of the handler not the handler itself. So your statement should looks like dojo.connect(coreData, "onLoad", buildLayerList). Unless you define the handler directly in your statement like this:
dojo.connect(coreData, "onLoad", function(coreData){
            /* by the way the argument coreData here
             * is just a reference of coreData, not the coreDate
             * argument in dojo.connect itself
             * so it is confusing, should use somthing like layer
             */
            var infos = coreData.layerInfos;
            for (var i = 0; i < infos.length; i++) {
               ......
            }
});
....
0 Kudos
RichardKaufholz
New Contributor
Hi,

Thanks for your inputs. I worked around it by calling an "init" function on the event, which in turn calls the "buildLayerList" function twice, each with relevant layers as parameters.

Cheers,
Richard.
0 Kudos