hey
In JS, variables can point to functions, so if you have
function foo() { /* do something */ }
bar = foo;
then this works:
bar(); // same as calling foo()
So, you can pass functions into functions, and call them at any point.
So if you modify addButtons like this:
function addButtons(btnId, className, callback) { // callback is a
function
// ... stuff here ...
google.maps.event.addDomListener(oButton, 'click', function() {
callback(); // will call callback()
});
}
then you would call addButton like this:
addButtons(aCtrls[i], 'btnContainerChild', function() {
gmapSetCenter(defaultCenter);
});
----------------
More info:
Optionally, you could simplify the line with addDomListener like this:
google.maps.event.addDomListener(oButton, 'click', callback); // will
call callback on click
but that's less flexible than the original way, which allows you to
pass parameters when you call callback(), if you need to. Like this:
callback(oButton) // pass the button as param
Even more info:
The previous line could also be written in two other forms
callback.call(someObject, oButton);
callback.apply(someObject, [oButton]);
On May 30, 3:39 am, Sammy <[email protected]> wrote:
> Hi all
> I'm adding buttons to my map through a loop. I pass some parameters,
> and the addButtons() function creates the buttons. How can I pass the
> "task" for the button to the function? In the addListener line, I'm
> now writing the function name literally, I'd like find a way with a
> parameter.
> Thank you..
>
> // The loop...
> for (i=0;i<=4;i++) {
> oCC = addButtons(aCtrls[i],'btnContainerChild');
> ...
> }
>
> // The function...
> function addButtons(cButtonID,cClassName) {
> var oButton = document.createElement('DIV');
> oButton.className=cClassName;
> oButton.id=cButtonID;
> //... other settings here...
>
> // ===> Here, I'd like to set the click function through a parameter
> google.maps.event.addDomListener(oButton, 'click', function()
> {gmapSetCenter(defaultCenter)});
>
>
>
>
>
>
>
> }
--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" 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-maps-js-api-v3?hl=en.