Huh. I am still getting zero values added to the myStations object. I think my syntax for adding to an object is wrong. Any suggestions anyone?
On Feb 27, 8:43 am, tatlar <robertlnew...@gmail.com> wrote: > Thanks Stephan - I will take a look at your suggestion. Thanks for the > heads up about the return statement - PHP function habits die hard.... > > On Feb 27, 12:38 am, Stephan Veigl <stephan.ve...@gmail.com> wrote: > > > Hi, > > > first of all, AJAX is _asynchronous_, do you have any mechanism that > > ensures that the stations callback is executed _before_ the > > _dataloggers_ callback? (e.g. requesting datalogger once myStations > > has been filled) > > > If you use myStations in both callbacks is should be a global > > variable. (I know, global variables are no good programming practice. > > So either put all your global variables into one global object to > > reduce namespace pollution or have it global in the scope of the > > callbacks). > > > Would the additional "snet": "TA" variable hurt in myStations? > > If not, simply link the json result to myStations: > > > var myStations = {} ; > > $.getJSON("path/to/stations.js", function(stalist) { > > $.each(stalist.stations, function(staname,stavalues){ > > var myStaTitle = stavalues.snet+"_"+staname ; > > myStations[myStaTitle] = stavalues; > > }); > > > }); > > > I also noticed that you have a return statement in your callback. For > > whom this return should be? The callback is executed within jQuery on > > success of your AJAX request. jQuery does not know of your return > > value. > > > by(e) > > Stephan > > > 2009/2/27 tatlar <robertlnew...@gmail.com>: > > > > Attempt to post again..... > > > > Hi All, > > > > I have a JSON object (called 'dataloggers') that I am retrieving via > > > $.getJSON, and then creating a <table> dynamically with the results . > > > I can parse it just fine with $.each(). > > > > The structure looks like this: > > > > { > > > "dataloggers": { > > > "TA_124A": { > > > "values": { > > > "one": "wake", > > > "two": "up", > > > "three": "time", > > > "four": "to", > > > "five": "die" > > > } > > > }, > > > "TA_109C": { > > > "values": { > > > "one": "this", > > > "two": "is", > > > "three": "not", > > > "four": "a", > > > "five": "test" > > > } > > > } > > > } > > > } > > > > Here is the code I have for processing the 'dataloggers' JSON object: > > > > $.getJSON("path/to/dataloggers.js", function(dlmon) { > > > $.each(dlmon.dataloggers, function(dlname,arr){ > > > var tBodyRow = "<tr>"; > > > tBodyRow += '<td>'+dlname+'</td>' ; > > > $.each(arr.values, function(vKey,vVal){ > > > tBodyRow += '<td>'+vKey+': '+vVal+'</td>' ; > > > }); > > > tBodyRow += "</tr>" ; > > > $("table#dataloggers tbody").append(tBodyRow); > > > }); > > > }); > > > > This outputs a table: > > > > <table id="dataloggers"> > > > <thead></thead> > > > <tfoot></tfoot> > > > <tbody> > > > <tr> > > > <td>TA_124A</td> > > > <td>one: wake</td> > > > <td>two: up</td> > > > <td>three: time</td> > > > <td>four: to</td> > > > <td>five: die</td> > > > </tr> > > > <tr> > > > <td>TA_109C</td> > > > <td>one: this</td> > > > <td>two: is</td> > > > <td>three: not</td> > > > <td>four: a</td> > > > <td>five: test</td> > > > </tr> > > > </tbody> > > > </table> > > > > I have another JSON object (called 'stations') that I am retrieving > > > via $.getJSON(), that has the following structure: > > > > { > > > "stations":{ > > > "124A":{ > > > "commtype":"slink2orb", > > > "provider":"orb", > > > "snet": "TA" > > > }, > > > "109C":{ > > > "commtype": "vsat", > > > "provider": "Verizon", > > > "snet": "TA" > > > } > > > } > > > } > > > > What I need to do is process the 'stations' JSON object and add the > > > values returned to the processing of the 'dataloggers' object with the > > > $.each() function, so that the HTML <table> output now looks like > > > this: > > > > <table id="dataloggers"> > > > <thead></thead> > > > <tfoot></tfoot> > > > <tbody> > > > <tr> > > > <td>TA_124A</td> > > > <td>one: wake</td> > > > <td>two: up</td> > > > <td>three: time</td> > > > <td>four: to</td> > > > <td>five: die</td> > > > <td>commtype: slink2orb</td> > > > <td>provider: orb</td> > > > </tr> > > > <tr> > > > <td>TA_109C</td> > > > <td>one: this</td> > > > <td>two: is</td> > > > <td>three: not</td> > > > <td>four: a</td> > > > <td>five: test</td> > > > <td>commtype: vsat</td> > > > <td>provider: Verizon</td> > > > </tr> > > > </tbody> > > > </table> > > > > You will notice that the 'stations' object has a key-val pair of > > > "snet":"TA", which makes it easy for me to match the 'dataloggers' key > > > by just concatenating the 'stations' key with the 'snet' value. So > > > what I *think* I need to do is process the 'stations' JSON object > > > first, and create a new jQuery object on the fly with all the values I > > > need, which I then pass into the $.each() processing of the > > > 'dataloggers' object. I tried to do this: > > > > $.getJSON("path/to/stations.js", function(stalist) { > > > var myStations = {} ; > > > $.each(stalist.stations, function(staname,stavalues){ > > > var myStaTitle = stavalues.snet+"_"+staname ; > > > var myStaVals = { "commtype": stavalues.commtype, "provider": > > > stavalues.provider } ; > > > var staObj.push(myStaTitle) = myStaVals ; // could be very > > > wrong to use the push() array method?? > > > }); > > > return myStations ; > > > }); > > > > I would have thought that I could then pass this newly created jQuery > > > object ('myStations') to the processing of the 'dataloggers' JSON > > > object, using the 'myStaTitle' match with the 'dlname' key. But every > > > attempt I have made has failed. For whatever reason (very likely my > > > syntax) I cannot create my custom jQuery object and then pass it on. > > > Something like?: > > > > $.getJSON("path/to/dataloggers.js", function(dlmon) { > > > $.each(dlmon.dataloggers, function(dlname,arr){ > > > var tBodyRow = "<tr>"; > > > tBodyRow += '<td>'+dlname+'</td>' ; > > > $.each(arr.values, function(vKey,vVal){ > > > tBodyRow += '<td>'+vKey+': '+vVal+'</td>' ; > > > }); > > > > // insert here?? why does this not work?? > > > tBodyRow += '<td>commtype: '+myStations.dlname.commtype+'</ > > > td>' ; > > > tBodyRow += '<td>provider: '+myStations.dlname.provider+'</ > > > td>' ; > > > > tBodyRow += "</tr>" ; > > > $("table#dataloggers tbody").append(tBodyRow); > > > }); > > > }); > > > > I hope this all makes sense. If anyone could give me a shove in the > > > right direction, I would be much obliged. All help much appreciated > > > and thanks in advance.