Connecting Tech Pros Worldwide Forums | Help | Site Map

OOP: dynamic arguments in callback functions?

Tyler
Guest
 
Posts: n/a
#1: May 21 '06
Hi all,

I am using some components from a JS library (scriptalicious), where
callback functions are arguments to the constructors of the components
like so:

someClass x (name)
{
this.name = name;
this.slider = new
Control.Slider(this.name+'slide',this.name+'track' ,{
onSlide:function(v){foo()},
onChange:function(v){bar(v)}
);
}
Since I would like to dynamicaly create these components at runtime, I
would like the engine (the browser) to evaluate the value of a member
variable (this.name) and to pass this to the callback of the event,
like
.... onChange:function(v){bar(this.name)} ...
Is this possible by some escaping mechanism or so? (and am I making
myself clear enough?)?

Thanks a lot in advance


Ian Collins
Guest
 
Posts: n/a
#2: May 21 '06

re: OOP: dynamic arguments in callback functions?


Tyler wrote:[color=blue]
> Hi all,
>
> I am using some components from a JS library (scriptalicious), where
> callback functions are arguments to the constructors of the components
> like so:
>
> someClass x (name)
> {
> this.name = name;
> this.slider = new
> Control.Slider(this.name+'slide',this.name+'track' ,{
> onSlide:function(v){foo()},
> onChange:function(v){bar(v)}
> );
> }
> Since I would like to dynamicaly create these components at runtime, I
> would like the engine (the browser) to evaluate the value of a member
> variable (this.name) and to pass this to the callback of the event,
> like
> .... onChange:function(v){bar(this.name)} ...
> Is this possible by some escaping mechanism or so? (and am I making
> myself clear enough?)?
>[/color]
If you bind the information you will to provide to the callcack function
to the element with the callback, you can access it through the event.

Something like

element.info = {name:this.name};
element.onChange = callback;

function callback( event )
{
// x-browser code to get the event.

var info = event.target.info;
};

--
Ian Collins.
Tyler
Guest
 
Posts: n/a
#3: May 22 '06

re: OOP: dynamic arguments in callback functions?


Hi Ian,

thank you for the very handy tip, this is a nice one!
(I have solved my problem differently in the meantime by sending a
backpointer to the calling class instance to the callback, which was
an inbuild option in the nice scriptalicious slider)
But anyhow, this is an interesting method,
Thank you for your suggestion

Cheers

Closed Thread