Re: Callback looping heck

25 views
Skip to first unread message

asgallant

unread,
Sep 21, 2012, 11:17:19 AM9/21/12
to google-visua...@googlegroups.com
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

Jeff Bean

unread,
Sep 21, 2012, 3:51:21 PM9/21/12
to google-visua...@googlegroups.com
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

asgallant

unread,
Sep 21, 2012, 4:49:50 PM9/21/12
to google-visua...@googlegroups.com
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 is a fairly good explanation).
Reply all
Reply to author
Forward
0 new messages