Ticket #2528168 (closed enhancement)

Reporter


bzbugs
Opened: 07/7/09
Last modified: 09/14/09
Status: closed
Type: enhancement
Resolution: fixed

Owner


Todd Kloots
Target Release: 2.8.0
Priority: P3 (normal)
Summary: YAHOO.util.Event.delegate
Description:

imported from bugzilla ticket 1537360

Original description
by Isaac Schlueter at 2007-10-15 13:51

It would be nice if there was a handy way to use event delegation with the Event util. Here's what I'm thinking:

YAHOO.util.Event.delegate = function (testFn, ev, fn, scope, override, attachTo) {
return YAHOO.util.Event.on( attachTo || document.documentElement, ev, function (e,sc) {
el = YAHOO.util.Event.getTarget(e);
if ( testFn(el) ) {
fn.call((override ? this : el), e, sc);
}
}, scope, override);
};

testFn is a function that returns true or false based on whether or not the event should happen on that element. For example:

YAHOO.util.Event.delegate( function (el) { return el.tagName=='LI'; }, 'click', function () {
YAHOO.util.Dom.addClass('selected'); } );

Then, clicking on any LI on the page will add the className "selected".

Perhaps, it would make sense to extend the addListener/on function to take a function as the first argument.

Type: enhancement Observed in Version: 2.7.0
Component: Event Severity: S3 (normal)
Assigned To: Todd Kloots Target Release: 2.8.0
Location: Library Code Priority: P3 (normal)
Tags: Relates To:
Browsers: N/A
URL:
Test Information:

Change History

George

YUI Developer

Posted: 07/7/09

Comment 1 by Isaac Schlueter at 2007-10-15 14:03:05

Just realized that there's a bug in that suggestion, because it won't fire for events that occur on child nodes of the
ones you want.

This works better, while still only firing once:

YAHOO.util.Event.delegate = function (testFn, ev, fn, scope, override, attachTo) {
attachTo = YAHOO.util.Dom.get(attachTo) || document.documentElement;
return YAHOO.util.Event.on( attachTo, ev, function (e,sc) {
el = YAHOO.util.Event.getTarget(e);
el = YAHOO.util.Dom.getAncestorBy(testFn,el);
if ( el ) {
return fn.call((override ? this : el), e, sc);
}
}, scope, override);
};

George

YUI Developer

Posted: 07/7/09

Comment 2 by Adam Moore at 2007-10-15 15:06:45

In 3.0, the non-required parameters for addListener will likely be moved to an optional config object, so addListener
could be overloaded by adding this as a config option.

I will also investigate including the delegate function as provided in the 2.4.0 release.

George

YUI Developer

Posted: 07/7/09

Comment 3 by Adam Moore at 2008-02-13 10:08:19

A blog entry about the implementation of the feature on sun.com:
http://blogs.sun.com/greimer/entry/a_look_at_our_new

George

YUI Developer

Posted: 07/7/09
  • summary changed from [bz 1537360] Enhancement: YAHOO.util.Event.delegate to YAHOO.util.Event.delegate

Adam Moore

YUI Contributor

Posted: 07/8/09
  • milestone changed to 3.0.0 GA
  • status changed from new to accepted

Implemented in YUI3, but it currently is limited to selectors that should match the target's descendants (this was the most interesting use case). Leaving open to determine if we need to also support custom validator functions in place of a selector.

Adam Moore

YUI Contributor

Posted: 07/8/09

Adam Moore

YUI Contributor

Posted: 07/8/09
  • milestone changed to 2.8.0
  • version changed to 2.7.0

Adam Moore

YUI Contributor

Posted: 07/24/09

Todd Kloots

YUI Contributor

Posted: 07/28/09
  • status changed from assigned to accepted

Todd Kloots

YUI Contributor

Posted: 08/1/09
  • status changed from accepted to checkedin

Todd Kloots

YUI Contributor

Posted: 08/1/09

Adam -

This change is checked in as the "event-delegate" Event submodule. I have added a test case and the YUI Loader metadata has been updated as well. All that remains is for me to update the Event landing page.

- Todd

George

YUI Developer

Posted: 09/14/09
  • status changed from checkedin to closed

2.8.0 has been released. All "checkedin" items are available for download in the official release. Status of "checkedin" items is being set to closed.

George

YUI Developer

Posted: 09/14/09
  • resolution changed to fixed