Connecting a callback action listener WITH parameters

K

K Viltersten

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?
 
D

david.karr

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?
 
Á

Álvaro G. Vicario

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:

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/doc...ference:Functions#Functions_as_event_handlers
 
K

K Viltersten

When i'm using the following, everything works well.
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. :)
 
D

david.karr

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 ...
 
P

P. Prikryl

You can use something like this:

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

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

Thomas 'PointedEars' Lahn

Ãlvaro G. Vicario said:
K Viltersten escribió:

According to Core JavaScript 1.5 Reference, you cannot:

The Reference has zero relevance to this question.
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/doc...ference:Functions#Functions_as_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
 
K

K Viltersten

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?
 
P

P. Prikryl

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.
 
Ã

Ãlvaro G. Vicario

Thomas 'PointedEars' Lahn escribió:
The Reference has zero relevance to this question.


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.

So the syntax to assign a callback function with parameters is... :-?
 
K

K Viltersten

Do you mean that switching between quotation
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.

Oh, NOW i see it. It took quite some time (and your
explaination) before it rang the bell. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top