Create and manage a DOM event delegator.
FT's dom delegate library is a simple, easy-to-use component for binding to events on all target elements matching the given selector, irrespective of whether anything exists in the DOM at registration time or not. This allows developers to implement the event delegation pattern.
FT DOM Delegate is developed by FT Labs, part of the Financial Times.
The library has been deployed as part of the FT Web App and is tried and tested on the following browsers:
- Safari 5 +
- Mobile Safari on iOS 3 +
- Chrome 1 +
- Chrome on iOS 5 +
- Chrome on Android 4.0 +
- Opera 11.5 +
- Opera Mobile 11.5 +
- Firefox 4 +
- Internet Explorer 9 +
- Android Browser on Android 2 +
- PlayBook OS 1 +
When combined with a good event listener polyfill the library can be used in Internet Explorer 8 for events that bubble natively.
Get the browserify-able source from a package manager:
npm install dom-delegate
bower install dom-delegate
Download the built version. (Note: this exposes the API via
The library is written in CommonJS and so can either be
require in or the prebuilt wzrd.in version can be used, which makes the API available via
The script must be loaded prior to instantiating a Delegate object.
To instantiate Delegate on the
body and listen to some events:
// Do some things// Do some other thingswindowaddEventListener'load'var delegate = documentbody;delegateon'click' 'button' handleButtonClicks;// Listen to all touch move// events that reach the bodydelegateon'touchmove' handleTouchMove;false;
A cool trick to handle images that fail to load:
thisstyledisplay = 'none';windowaddEventListener'load'var delegate = documentbody;delegateon'error' 'img' handleImageFail;false;
Note: as of 0.1.2 you do not need to provide a DOM element at the point of instantiation, it can be set later via the
Also note: as of 0.2.0 you cannot specify more than one
eventType in a single call to
Delegate supports compilation with
ADVANCED_OPTIMIZATIONS ('advanced mode'), which should reduce its size by about 70% (60% gzipped). Note that exposure of the
Delegate variable isn't forced therefore you must compile it along with all of your code.
Tests are run using buster and sit in
test/. To run the tests statically:
$ cd ftdomdelegate/$ ./node_modules/.bin/buster-static -c test/buster.jsStarting server on
...then point your browser to http://localhost:8282/.
$ ./node_modules/.bin/buster-serverbuster-server running on
Point your browser to http://localhost:1111 and capture it, then in another terminal tab:
$ ./node_modules/.bin/buster-test -c test/buster.js
Code coverage is generated automatically with istanbul. The report outputs to
The event to listen for e.g.
Any kind of valid CSS selector supported by
matchesSelector. Some selectors, like
tag will use optimized functions internally that check for straight matches between the ID or tag name of elements.
null is also accepted and will match the root element set by
root(). Passing a handler function into
.on's second argument is equivalent to
.on(eventType, null, handler).
Function that will handle the specified event on elements matching the given selector. The function will receive two arguments: the native event object and the target element, in that order.
Whether or not to listen during the capturing (pass in
true) or bubbling phase (pass in
false). If no value passed in, it will fallback to a 'sensible default', which is
focus events and
false for all other types.
off with no arguments will remove all registered listeners, effectively resetting the instance.
Remove handlers for events matching this type considering the other parameters.
Only remove listeners registered with the given selector, among the other arguments.
If null passed listeners registered to the root element will be removed. Passing in a function into
off's second parameter is equivalent to
.off(eventType, null, handler[, useCapture]) (the third parameter will be ignored).
Only remove listeners registered with the given handler function, among the other arguments. If not provided, remove all handlers.
Only remove listeners with
useCapture set to the value passed in. If not provided, remove listeners added with
useCapture set to
Set the delegate's root node. If no element passed in the root node will be deleted and the event listeners will be removed.
Short hand for off() and root(), ie both with no parameters. Used to reset the delegate object.
The developers of ftdomdelegate are Matthew Andrews and Matthew Caruana Galizia. Test engineering by Sam Giles. The API is influenced by jQuery Live. All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request.