HighDots Forums  

Connecting a callback action listener WITH parameters

Javascript JavaScript language (comp.lang.javascript)


Discuss Connecting a callback action listener WITH parameters in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
K Viltersten
 
Posts: n/a

Default Connecting a callback action listener WITH parameters - 05-22-2008 , 09:47 AM






When i'm using the following, everything works well.

butt1.on ('click', function () {sameStuff ('1')});
butt2.on ('click', function () {sameStuff ('2')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i'm required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on ('click', function ('1'));

How can i enter as the second parameter BUT with a specified parameter in
it? Is it doable at all?

--
Regards
Konrad Viltersten

Reply With Quote
  #2  
Old   
david.karr
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-22-2008 , 10:32 AM






On May 22, 7:47 am, "K Viltersten" <t... (AT) viltersten (DOT) com> wrote:
Quote:
When i'm using the following, everything works well.

butt1.on ('click', function () {sameStuff ('1')});
butt2.on ('click', function () {sameStuff ('2')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i'm required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on ('click', function ('1'));

How can i enter as the second parameter BUT with a specified parameter in
it? Is it doable at all?
I'm having trouble understanding what you're trying to do.

Are you perhaps trying to define a single function that can act as the
handler for both button clicks, and do something useful? If that's
the case, just define the function before the two calls to "on",
assigning it to a variable, and reference that variable in the two
"on" calls.

In the common event handler, you'll probably have to know which button
was clicked, so you'll have to get the event object (either passed in
as the single parameter, or the global event object) and get the
target property.

Does that help?


Reply With Quote
  #3  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-22-2008 , 11:09 AM



K Viltersten escribió:
Quote:
When i'm using the following, everything works well.

butt1.on ('click', function () {sameStuff ('1')});
butt2.on ('click', function () {sameStuff ('2')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i'm required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on ('click', function ('1'));

How can i enter as the second parameter BUT with a specified parameter
in it? Is it doable at all?
According to Core JavaScript 1.5 Reference, you cannot:

To pass parameters to an event handler, the handler must be
wrapped into another function"

document.form1.button1.onclick = function() {
setBGColor('some value');
};

http://developer.mozilla.org/en/docs...event_handlers


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--


Reply With Quote
  #4  
Old   
K Viltersten
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-22-2008 , 12:28 PM



Quote:
When i'm using the following, everything works well.
butt1.on ('click', function () {sameStuff ('1')});
butt2.on ('click', function () {sameStuff ('2')});
Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i'm required to send in a function as
the second parameter. If i use the following i get only a return back,
not typed as function.
butt1.on ('click', function ('1'));
How can i enter as the second parameter BUT with a specified parameter
in it? Is it doable at all?

According to Core JavaScript 1.5 Reference, you cannot:

To pass parameters to an event handler, the handler must be
wrapped into another function"

document.form1.button1.onclick = function() {
setBGColor('some value');
};

http://developer.mozilla.org/en/docs...event_handlers
Too bad! Thanks.

--
Regards
Konrad Viltersten


Reply With Quote
  #5  
Old   
K Viltersten
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-22-2008 , 12:29 PM



Quote:
When i'm using the following, everything works well.

butt1.on ('click', function () {sameStuff ('1')});
butt2.on ('click', function () {sameStuff ('2')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i'm required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on ('click', function ('1'));

How can i enter as the second parameter BUT with a specified parameter
in
it? Is it doable at all?

I'm having trouble understanding what you're trying to do.

Are you perhaps trying to define a single function that can act as the
handler for both button clicks, and do something useful? If that's
the case, just define the function before the two calls to "on",
assigning it to a variable, and reference that variable in the two
"on" calls.

In the common event handler, you'll probably have to know which button
was clicked, so you'll have to get the event object (either passed in
as the single parameter, or the global event object) and get the
target property.

Does that help?
Nope. That, i had already. I wanted to do something even more compact. As
mr Vicario pointed out, i will not get to do it the way i wanted. Thanks
for the reply anyhow. It's appreciated.

--
Regards
Konrad Viltersten


Reply With Quote
  #6  
Old   
david.karr
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-22-2008 , 01:54 PM



On May 22, 7:47 am, "K Viltersten" <t... (AT) viltersten (DOT) com> wrote:
Quote:
When i'm using the following, everything works well.

butt1.on ('click', function () {sameStuff ('1')});
butt2.on ('click', function () {sameStuff ('2')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i'm required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on ('click', function ('1'));

How can i enter as the second parameter BUT with a specified parameter in
it? Is it doable at all?
Now that I understand what you're trying to do, note that YUI, and
likely all of the major JS frameworks, make it easy to associate
additional parameters with specific handler bindings. It's not worth
doing this if that's all you need it for, but if you were making a
list of reasons ...


Reply With Quote
  #7  
Old   
P. Prikryl
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-22-2008 , 02:01 PM



You can use something like this:

function makeSameStuff(n) {
return function() { sameStuff(n); };
}

butt1.on("click", makeSameStuff("1"));
butt2.on("click", makeSameStuff("2"));

Reply With Quote
  #8  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-22-2008 , 05:27 PM



Ãlvaro G. Vicario wrote:
Quote:
K Viltersten escribió:
When i'm using the following, everything works well.

butt1.on ('click', function () {sameStuff ('1')});
butt2.on ('click', function () {sameStuff ('2')});

Now, i wish to compact it into one call, instead of two anonymous
functions. The problem is that i'm required to send in a function as the
second parameter. If i use the following i get only a return back, not
typed as function.

butt1.on ('click', function ('1'));

How can i enter as the second parameter BUT with a specified parameter
in it? Is it doable at all?

According to Core JavaScript 1.5 Reference, you cannot:
The Reference has zero relevance to this question.

Quote:
To pass parameters to an event handler, the handler must be
wrapped into another function"

document.form1.button1.onclick = function() {
setBGColor('some value');
};

http://developer.mozilla.org/en/docs...event_handlers
This section should be removed as it advocates an obsolete practice
and has nothing to do with the ("Core") programming language. Event
handlers/listeners are part of the (Gecko) DOM API.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


Reply With Quote
  #9  
Old   
K Viltersten
 
Posts: n/a

Default SV: Connecting a callback action listener WITH parameters - 05-22-2008 , 11:33 PM



Quote:
You can use something like this:

function makeSameStuff(n) {
return function() { sameStuff(n); };
}

butt1.on("click", makeSameStuff("1"));
butt2.on("click", makeSameStuff("2"));

Do you mean that switching between quotation
marks and apostrophes makes that difference?

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy



Reply With Quote
  #10  
Old   
P. Prikryl
 
Posts: n/a

Default Re: Connecting a callback action listener WITH parameters - 05-23-2008 , 02:13 AM



On 23. Máj, 06:33 h., "K Viltersten" <t... (AT) viltersten (DOT) com> wrote:
Quote:
Do you mean that switching between quotation
marks and apostrophes makes that difference?
No, it does not matter whether you use single or double quotes. The
difference is the function makeSameStuff, which returns function that
is used as second argument of butt1.on and butt2.on.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.