I have a problem passing variables through infoTemplate. Its like there's something funny happening with the scope. Certain variables work but when I get too crazy with them my map doesn't load.

Example1 works:

template = new InfoTemplate({
    title: welltitle,
    content: "ID: "+coords[i][0]+"<br />"+coords[i][0]+"<br /><a href='http://waterdata.usgs.gov/nwis/dv/?site_no="+coords[i][0]+"&agency_cd=USGS&amp;referred_module=sw' target='_blank'><img width='100%' src='http://waterdata.usgs.gov/nwisweb/graph?agency_cd=USGS&site_no="+coords[i][0]+"&parm_cd=72019&period=7' /></a>"+datatext

Example2 does not:

template = new InfoTemplate({
    title: welltitle,
    content: "ID: "+coords[i+1][0]+"<br />"+coords[i+1][0]+"<br /><a href='http://waterdata.usgs.gov/nwis/dv/?site_no="+coords[i+1][0]+"&agency_cd=USGS&amp;referred_module=sw' target='_blank'><img width='100%' src='http://waterdata.usgs.gov/nwisweb/graph?agency_cd=USGS&site_no="+coords[i+1][0]+"&parm_cd=72019&period=7' /></a>"+datatext

EDIT coords is a multidimensional array of values

EDIT NEW SYMPTOM! map crashes in firefox but not Chrome...this may be just a cross-browser issue

  • Since this is in a loop, wouldn't it have an issue when i=coords.length and it tries to obtain i+1? – kenbuja Feb 26 '14 at 21:04

Ok, you don't for sure include enough info for me to know if this is the problem for sure or not, but I think the problem might be that you need to check if "coords" is an object or an array. If it is an array, I have no clue why this would be a problem and you should probably re-ask this as a javascript question on stack overflow. However, if coords is an object rather than an array, then your i+1 won't work. If you are iterating over all the values in an array (for i in coords), i will be the numeric place in the array, so +1 will move you over one place in the array. If you are iterating over all the values in an object however, i will be the named keys of the object. Example: in the case of the following object: coords = {lat:123.456, long:789.012} iterating over coords[i] would be the same as coords["lat"], and obviously coords["lat"+1] would the same as coords.lat1. Since there is no lat1 property in this case, it would return nothing and could potentially cause other errors in the application if there are dependencies on this running correctly.

If you are working with an object of coords rather than an array, I think you might want to look at maybe using something like the what is described here about getting a list of keys from an object as an array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys If you had an array of all the keys you could iterate over that and use the +1 there.

Ex: you have coords = {lat:123.456, long:789.012}

Object.keys(coords) should in theory return something like ["lat","long"]. So, you could store that as coordsKeys = Object.keys(coords). Then a for loop over coordsKeys with the loop var being i would make i=0 look like: coords[coordsKeys[i+1]] which should return a value of 789.012

All of this is theoretical of course though... I've not personally used this method as generally objects are used with named keys rather than using arrays because you want to access specific attributes of the object individually rather than in relation to each other as is often done with arrays.

Hope that helps, and remember, this sounds largely like a core javascript question rather than an ArcGIS specific question, so you might get some help from the StackOverflow site too...

| improve this answer | |
  • Sorry, should have included that this is an array. I just double checked and it gave me a list of values, so that confirms that its an array, not an object. Its also buried in lots of for loops and if statements. Would that have something to do with the scope? Is dojo/ARCGIS more sensitive to scope than standard javascript? – daniella Feb 26 '14 at 15:17
  • dojoArcGIS isn't more sensitive to scope as far as I know, but... If it's truly an array though, I'm not sure exactly what the problem is. I don't think it's a scope issue though really, because at least to me that would imply that the "i" would be the issue and it isn't, it's the +1 that's the issue per your example. What I would do first is just add some kind of alert/console-log to tell you what values "i" is representing just to double check (make sure it's a list of numbers - unless that's what you meant above.) Otherwise, sorry I can't help more... – John Feb 26 '14 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.