special partial/bind function for event listener ?

3 views
Skip to first unread message

bon...@gmail.com

unread,
Aug 25, 2005, 10:51:19 PM8/25/05
to MochiKit
Hi,

While reading this thread

http://groups.google.com/group/mochikit/browse_frm/thread/d2173653271b12bc/fe76c9b38818556c#fe76c9b38818556c

it leads me to this interesting link(thanks stedi for the finding)

http://www.brockman.se/writing/method-references.html

which is a special form of "partial+bind" that is for event listener
rather than generic function. The difference is that event listener
expect the first argument to be the "event" object that is passed when
the event is triggered. I find it a bit hard to do it using the
existing partial function(if I need to pass additional arguments to
it), The current "bind" function seems to be able to handle event
listener withe form of "func(event)".

Can this form of partial(may be the more generic form of from right to
left instead of the left to right) be added to MochiKit ?

Bob Ippolito

unread,
Aug 25, 2005, 11:26:53 PM8/25/05
to bon...@gmail.com, MochiKit

On Aug 25, 2005, at 7:51 PM, bon...@gmail.com wrote:

> While reading this thread
>
> http://groups.google.com/group/mochikit/browse_frm/thread/
> d2173653271b12bc/fe76c9b38818556c#fe76c9b38818556c
>
> it leads me to this interesting link(thanks stedi for the finding)
>
> http://www.brockman.se/writing/method-references.html
>
> which is a special form of "partial+bind" that is for event listener
> rather than generic function. The difference is that event listener
> expect the first argument to be the "event" object that is passed when
> the event is triggered. I find it a bit hard to do it using the
> existing partial function(if I need to pass additional arguments to
> it), The current "bind" function seems to be able to handle event
> listener withe form of "func(event)".

I don't quite understand what you mean by "The current bind function
seems...".

Anyway, bind is actually all you need, partial is actually just a
special case of bind for when you want to leave "this" alone. From
the MochiKit.Base docs:

partial is a special form of bind that does not alter the bound
self (if any). It is equivalent to calling:

bind(func, undefined, arg[, ...]);


(that wording should probably be changed to bound ``this``).

> Can this form of partial(may be the more generic form of from right to
> left instead of the left to right) be added to MochiKit ?

I don't see any reason to do it that way. Binding from right-to-left
is unnecessary. Here's what I would do (unoptimized and untested):

MochiKit.DOM.asEventListener = function (func) {
return function (e) {
func.call(this, e || event);
};
};

MochiKit.DOM.addEventListener = function (element, action, func) {
var listener = MochiKit.DOM.asEventListener(func);
element = MochiKit.DOM.getElement(element);
if (element.addEventListener) {
element.addEventListener(action, listener, false);
} else if (element.attachEvent) {
element.attachEvent("on" + action, listener);
}
return listener;
};

If you want to bind arguments or "this", do it with bind or partial.
I don't think every function that takes a callable needs to grow that
functionality... The event will simply be the last argument that you
receive.

function myListener(arg1, arg2, event) {
assert(this == someElement);
assert(arg1 == 1);
assert(arg2 == 2);
assert(event);
};

addEventListener(someElement, "action", partial(myListener, 1, 2));

This should work as you'd expect it to work.

-bob

bon...@gmail.com

unread,
Aug 25, 2005, 11:48:18 PM8/25/05
to MochiKit
thanks for the quick reply.

I think this is what I need. The reason why I mentioned right to left
partial is that I just follow the convention of event listener usually
has the first argument as event, having it as the last should serve the
same purpose(passing argument to event listener), just that
newbies(like me) may find it unusual(puzzled) when all google returns
is in the other form.

Will this asEventListener and addEventListener go into the next release
of MochiKit ?

Bob Ippolito

unread,
Aug 26, 2005, 12:28:10 AM8/26/05
to bon...@gmail.com, MochiKit

On Aug 25, 2005, at 8:48 PM, bon...@gmail.com wrote:

>
> thanks for the quick reply.
>
> I think this is what I need. The reason why I mentioned right to left
> partial is that I just follow the convention of event listener usually
> has the first argument as event, having it as the last should serve
> the
> same purpose(passing argument to event listener), just that
> newbies(like me) may find it unusual(puzzled) when all google returns
> is in the other form.

The traditional form for event listeners also has event as the last
argument -- they ONLY have one argument.

> Will this asEventListener and addEventListener go into the next
> release
> of MochiKit ?

Yeah, I'll put those in the next release unless someone has any good
reason to rename them or change the API (speak up if you do!). It'll
probably be a day or two before they land in svn, I've got a backlog
of other non-JavaScript things I really need to take care of right now.

-bob

Thomas Hervé

unread,
Aug 27, 2005, 5:21:08 AM8/27/05
to MochiKit
Bob Ippolito wrote:
> I don't see any reason to do it that way. Binding from right-to-left
> is unnecessary. Here's what I would do (unoptimized and untested):
> [snip]

It works for me.

> If you want to bind arguments or "this", do it with bind or partial.
> I don't think every function that takes a callable needs to grow that
> functionality... The event will simply be the last argument that you
> receive.

Agree, but a workaround function can be useful. Otherwise, adding a
method as listener make it like this :
MochiKit.DOM.addEventListener(element, 'action',
MochiKit.DOM.asEventListener(MochiKit.Base.bind(this.handler, this)));
(using global namespace).

I personnaly use this :

MochiKit.DOM.addMethodEventListener = function (element, action, func,
self) {
MochiKit.DOM.addEventListener(element, action,

MochiKit.DOM.asEventListener(MochiKit.Base.bind(func, self)));
};

It could be good to have it in the docs.

--
Thomas

Bob Ippolito

unread,
Aug 27, 2005, 6:09:21 AM8/27/05
to Thomas Hervé, MochiKit

On Aug 27, 2005, at 2:21 AM, Thomas Hervé wrote:

>
> Bob Ippolito wrote:
>
>> I don't see any reason to do it that way. Binding from right-to-left
>> is unnecessary. Here's what I would do (unoptimized and untested):
>> [snip]
>>
>
> It works for me.
>
>
>> If you want to bind arguments or "this", do it with bind or partial.
>> I don't think every function that takes a callable needs to grow that
>> functionality... The event will simply be the last argument that you
>> receive.
>>
>
> Agree, but a workaround function can be useful. Otherwise, adding a
> method as listener make it like this :
> MochiKit.DOM.addEventListener(element, 'action',
> MochiKit.DOM.asEventListener(MochiKit.Base.bind(this.handler, this)));
> (using global namespace).

asEventListener won't really be a very commonly used function, since
it would be called automatically by addEventListener. It is only
useful to people who want to write their own addEventListener
equivalent, so it would be incorrect to use it here.

-bob

Thomas Hervé

unread,
Aug 27, 2005, 10:54:01 AM8/27/05
to MochiKit

Bob Ippolito wrote:
> asEventListener won't really be a very commonly used function, since
> it would be called automatically by addEventListener. It is only
> useful to people who want to write their own addEventListener
> equivalent, so it would be incorrect to use it here.

Sorry, misreading, I didn't see the asEventListener call in the
addEventListener function.

Please forget last post.

--
Thomas

Reply all
Reply to author
Forward
0 new messages