Ticket #2528168 (closed enhancement)
Reporterbzbugs |
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 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) { 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 () { 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
|
Posted: 07/7/09
|
|
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 I will also investigate including the delegate function as provided in the 2.4.0 release. |
|
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: |
|
Posted: 07/7/09
|
|
Posted: 07/8/09
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. |
|
Posted: 07/8/09
Ticket copied from: http://yuilibrary.com/projects/yui3/ticket/2528044 |
|
Posted: 07/8/09
|
|
Posted: 07/24/09
|
|
Posted: 07/28/09
|
|
Posted: 08/1/09
|
|
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 |
|
Posted: 09/14/09
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. |
|
Posted: 09/14/09
|
Adam Moore
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);
};