My documentation skills leave much to be desired, so if there is anything 
you want clarified, feel free to ask.

The specific instance you mentioned is a closure.  Each iteration through 
the loop, you want to pass the value of i into the function, but by 
default, javascript uses whatever the value of a variable is at the time a 
piece of code is executed, not what it was at the time the piece is 
instantiated.  In this case, since the query function sends out an AJAX 
request, and the callback isn't executed until the request returns (which 
is likely to happen long after the loop is finished), the value of "i" 
would be the same for all instances of the callback.  To fix this, we use a 
closure, which locks the value of i in each iteration to the variable x 
inside the closure, giving each callback a unique value of x.  The closure 
is executed immediately, which is why we have to return the actual callback 
function inside the closure.

Does that make sense?  Google "js closure" to get a better idea of what 
they do and how to use them 
(this<http://stackoverflow.com/questions/61088/hidden-features-of-javascript/2047391#2047391>is
 a fairly good explanation).

On Friday, September 21, 2012 3:51:21 PM UTC-4, Jeff Bean wrote:
>
> Thank you!
>
> I was aware of the loop error, but the "declare an inline function as a 
> paramater and pass the table ID to it and then pass it in to the callback" 
> trick totally lost me. This is an invaluable, albeit a bit cryptic, example 
> that I was able to get working on my real example.
>
> Thanks again!
>
> Jeff
>
> On Friday, September 21, 2012 8:17:19 AM UTC-7, asgallant wrote:
>>
>> You almost had it, but there were a couple things you needed to fix:
>>
>> 1) you were looping over 5 characters when the string length is 4, so the 
>> last iteration would throw an error.
>> 2) every iteration, you would put the output in the same div ("table"), 
>> so no matter how many queries were successful, each one would overwrite the 
>> results of the previous one.
>>
>> The first is easy to fix - just base to loop length on the string length 
>> instead of a fixed value.
>>
>> The second takes a bit of code reorganization, as you have to pass some 
>> value that allows the drawing code to know which div to stick the results 
>> in.  Here's one way to approach the problem: 
>> http://jsfiddle.net/asgallant/az3wB/
>>
>> On Friday, September 21, 2012 1:18:55 AM UTC-4, Jeff Bean wrote:
>>>
>>> So I have a spreadsheet:
>>>
>>>
>>> https://docs.google.com/spreadsheet/ccc?key=0AowPbANxu0u2dGJFNXJhNVhLLTNSVlRtMGJGWXQxQVE
>>>
>>> I'd like to use the visualization API to auto-generate a document as 
>>> follows:
>>>
>>> For each column myCol on the page, create a list L
>>>  For each row R
>>>    include the value from column A if myCol, R is set to X.
>>>
>>> You'd think this would be easy but I'm having a hard time. I can 
>>> generate one list for one column, but I can't loop through and generate all 
>>> the lists on the same page. 
>>>
>>> My broken code is attached. Any ideas?
>>>
>>> Jeff
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-visualization-api/-/xmkTjBQe3S4J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-visualization-api?hl=en.

Reply via email to