Stupid me. The code I wrote you actually executes the function (returning
nothing, hence the undefined error you get):
(function(x) { ... })(x);
while instead should return one. So either you change it to do so:
for (x in charts) {
listener[x] = g.v.events.addListener(charts[x]['chart'], 'ready' ,
(function(x) {
return function() {
// do something with 'x'
};
})(x));
...
}
but that becomes really verbose. So you can lock the 'x' just inside the
for loop and achieve the same result in a slightly more compact way:
for (x in charts) {
(function(x) { // 'x' is now locked by the closure
g.v.events.addListener(charts[x]['chart'], 'ready', function() { /*
your listener code */ });
})(x);
}
Again, haven't tried the above in a live example, so let's hope I didn't
write something wrong again :-).
Another reference reading, for better understanding:
http://benalman.com/news/2010/11/immediately-invoked-function-expression/
( look at the 'saving state with closures' paragraph, that describes
exactly the 2 techniques I mentioned above).
- R.
On 4 January 2012 15:56, asgallant <[email protected]> wrote:
> Dude, you rock!
>
> I suppose this thread should be renamed "weird behavior in Chrome/IE,"
> then. So, doing as you suggest, the function executes properly in all
> browsers. The problem now is that they all throw errors when you pass a
> closure to #addListener():
>
> FF:
> Error: a is not a function
> Source File:
> http://www.google.com/uds/api/visualization/1.0/6ceef8f4137a389d7745e173bfc981e5/format+en,default,corechart.I.js
> Line: 170
>
> Chrome:
> "undefined is not a function"
>
> IE:
> "Object expected"
>
> This occurs regardless of whether you pass the closure in a loop or not,
> and whether or not there is anything in the function itself. I plopped one
> of these into a jsfiddle as a test case: http://jsfiddle.net/zeXg6/
>
> I solved the problem by wrapping the #addListener call in the closure
> instead: http://jsfiddle.net/zeXg6/1/
>
> It looks ugly, but it works.
>
> --
> 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/-/0jUuXw-os_sJ.
>
> 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.
>
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
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.