bindAsEventListener() - is there an equivalent in jQuery?

173 views
Skip to first unread message

greenie2600

unread,
Aug 7, 2008, 3:11:41 PM8/7/08
to Prototype & script.aculo.us
Hi all -

I've played around with jQuery, and I've been very impressed. However,
I sorely miss Prototype's bindAsEventListener() when working with
jQuery.

Has anyone discovered a comparable function in jQuery? Prototype is
great, but I'd like to at least give jQuery a shot...

david

unread,
Aug 11, 2008, 7:49:56 AM8/11/08
to Prototype & script.aculo.us
Hi greenie2600,

You should go in a JQUERY forum to have this answer !
I'm not sure you will have a response in this groups.

--
David

Christophe Porteneuve

unread,
Aug 11, 2008, 10:53:45 AM8/11/08
to prototype-s...@googlegroups.com
AAMOF, jQuery doesn't have pre-baked binding. They ask their users to
understand the costs and dynamics of binding, and the concept of lexical
closure, so they "manually bind."

When it comes right down to it, I doubt the OP actually needs
@bindAsEventListener@, which is quite seldom useful. They probably use
it in a way where @bind@ is sufficient (i.e. without pre-filled
arguments to be passed after the event object).

--
Christophe Porteneuve aka TDD
t...@tddsworld.com

david

unread,
Aug 12, 2008, 8:05:41 AM8/12/08
to Prototype & script.aculo.us
Hi greenie2600,

I was wrong!
you'll have an answer !

--
David

jdalton

unread,
Aug 12, 2008, 10:00:37 AM8/12/08
to Prototype & script.aculo.us

If you aren't passing arguments to bindAsEventListener like,

Grid ={
otherMethod: function() {
//...
},
handler: function(arg1, arg2, arg3) {
this.otherMethod(arg2)
//...
}
};

$(document.body).observe('click',
Grid.handler.bindAsEventListener(Grid, "edit", "admin"));
// --> when document.body is clicked it will call the handler method
bound to Grid,
// so "this" is Grid, and pass the event object as arg1, "edit" as
arg2, and "admin" as arg3.

If your code looks like:
$(document.body).observe('click',
Grid.handler.bindAsEventListener(Grid));
// then you can totally use "bind" instead of "bindAsEventListener".

By default, in Prototype 1.6+ and jQuery, "this" of an event handler
is the element that you are observing.
In my previous examples it would be document.body. However using bind
I made "this" reference the Grid object.

AFAIK There isn't an equiv in jQuery, but then again the need is
pretty edge case.

http://www.prototypejs.org/api/function/bindAsEventListener
http://docs.jquery.com/Events/bind

- JDD
Reply all
Reply to author
Forward
0 new messages