Why can't I assign a layer to a value?

3982
6
Jump to solution
05-13-2015 02:21 PM
ChrisSergent
Regular Contributor III

I am trying to loop through layers and this block of code works:

 var jsonValue, signUrl, supportUrl;
    var restEndPoint = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/";
    var config;
    // Get the id of a layer
    var requestHandle = esriRequest({
        url: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/",
        content: {
            f: 'json'
        },
        handleAs: "json"
    });


    requestHandle.then(function (lyrJSON, io) {
        for (var i = 0; i < lyrJSON.layers.length; i++) {
            if (lyrJSON.layers.name = "Support") {
                jsonValue = lyrJSON.layers.id;
                supportUrl = restEndPoint + jsonValue;
            }
            
        }




        // app configuration  
        config = {






            mapOptions: {
                showAttribution: false,
                sliderStyle: "small",
                extent: initialExtent,
                logo: false,
                sliderPosition: "bottom-right"
            },


            signLayerUrl: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/0",


            supportLayerUrl: supportUrl


        };
        initMap();
        
    })

But when I try to add the following code, I can't assign the value to signLayerUrl. I even tried to change i to t for the second if statement:

 var signValue, signUrl, supportValue, supportUrl;
    var restEndPoint = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/";
    var config;
    // Get the id of a layer
    var requestHandle = esriRequest({
        url: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/",
        content: {
            f: 'json'
        },
        handleAs: "json"
    });


    requestHandle.then(function (lyrJSON, io) {
        for (var i = 0; i < lyrJSON.layers.length; i++) {
            if (lyrJSON.layers.name = "Support") {
                supportValue = lyrJSON.layers.id;
                supportUrl = restEndPoint + supportValue;
            }


            if (lyrJSON.layers.name = "Sign") {
                signValue = lyrJSON.layers.id;
                signUrl = restEndPoint + signValue;
            }
            
        }




        // app configuration  
        config = {






            mapOptions: {
                showAttribution: false,
                sliderStyle: "small",
                extent: initialExtent,
                logo: false,
                sliderPosition: "bottom-right"
            },


            signLayerUrl: signUrl,


            supportLayerUrl: supportUrl


        };
        initMap();
        
    })
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Your "if" statements are incorrect. When comparing values, you have to use "===" (or "==" if you're not worried about comparing types in addition)

if (lyrJSON.layers.name === "Support") {  

View solution in original post

6 Replies
KenBuja
MVP Esteemed Contributor

Your "if" statements are incorrect. When comparing values, you have to use "===" (or "==" if you're not worried about comparing types in addition)

if (lyrJSON.layers.name === "Support") {  

ChrisSergent
Regular Contributor III

Thanks. I was evaluating the assignment. Felt stupid. Works great.

0 Kudos
OwenEarley
Occasional Contributor III

Just a suggestion but you could also initialize the config object right at the start and then just assign the values to it. This just reduces the number of variables used and keeps things clean:

    // app configuration  
    var config = {
        mapOptions: {
            showAttribution: false,
            sliderStyle: "small",
            extent: initialExtent,
            logo: false,
            sliderPosition: "bottom-right"
        },  
        signLayerUrl: undefined,
        supportLayerUrl: undefined
    };
     
    restEndPoint = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/";

    // Get the id of a layer
    var requestHandle = esriRequest({
        url: restEndPoint,
        content: { f: 'json' },
        handleAs: "json"
    });

    requestHandle.then(function (lyrJSON, io) {
        for (var i = 0; i < lyrJSON.layers.length; i++) {
            if (lyrJSON.layers.name == "Support") {  
                config.supportLayerUrl = restEndPoint + lyrJSON.layers.id;
            }
            if (lyrJSON.layers.name == "Sign") {
                config.signLayerUrl = restEndPoint + lyrJSON.layers.id;
            }
        }
        initMap();
    })

EDIT - updated with the final function parenthesis and restEndPoint variable as Chris pointed out (just in case anyone copies the code block).

ChrisSergent
Regular Contributor III

Awesome. It did need a trailing parenthesis and restEndPoint to be declared still, but you knocked out four variables for me. Thanks!

Here is my updated code for anyone that needs it:

var restEndPoint = "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/";
   
    // app configuration    
    var config = {  
        mapOptions: {  
            showAttribution: false,  
            sliderStyle: "small",  
            extent: initialExtent,  
            logo: false,  
            sliderPosition: "bottom-right"  
        },    
        signLayerUrl: undefined,  
        supportLayerUrl: undefined  
    };  
  
  
    // Get the id of a layer  
    var requestHandle = esriRequest({  
        url: "http://maps.decaturil.gov/arcgis/rest/services/test/StreetSignTest/FeatureServer/",  
        content: { f: 'json' },  
        handleAs: "json"  
    });  
  
    requestHandle.then(function (lyrJSON, io) {  
        for (var i = 0; i < lyrJSON.layers.length; i++) {  
            if (lyrJSON.layers.name == "Support") {    
                config.supportLayerUrl = restEndPoint + lyrJSON.layers.id;  
            }  
            if (lyrJSON.layers.name == "Sign") {  
                config.signLayerUrl = restEndPoint + lyrJSON.layers.id;  
            }  
        }  
        initMap();  
    })  
0 Kudos
OwenEarley
Occasional Contributor III

Apart from just reducing the number of variables you can use this as a namespace pattern to avoid possible variable collisions as the code base gets much larger. This keeps the variables that you declare isolated from variables that may be used in other third party code.

There is some really good info on this page - Essential JavaScript Namespacing Patterns

ChrisSergent
Regular Contributor III

I added it on my toDo's for reading. Thanks.