
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
dom-delegate
Advanced tools
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.
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:
npm install dom-delegate
or
bower install dom-delegate
or
Download the production version (<1k gzipped) or the development version.
The script must be loaded prior to instantiating a Delegate object.
To instantiate Delegate on the body
and listen to some events:
function handleButtonClicks(event) {
// Do some things
}
function handleTouchMove(event) {
// Do some other things
}
window.addEventListener('load', function() {
var delegate = new Delegate(document.body);
delegate.on('click', 'button', handleButtonClicks);
// Listen to all touch move
// events that reach the body
delegate.on('touchmove', handleTouchMove);
}, false);
A cool trick to handle images that fail to load:
function handleImageFail() {
this.style.display = 'none';
}
window.addEventListener('load', function() {
var delegate = new Delegate(document.body);
delegate.on('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 root
method.
Also note: as of 0.2.0 you cannot specify more than one eventType
in a single call to off
or on
.
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.js
Starting server on http://localhost:8282/
...then point your browser to http://localhost:8282/.
$ ./node_modules/.bin/buster-server
buster-server running on http://localhost:1111
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 lcov-report/index.html
.
eventType (string)
The event to listen for e.g. mousedown
, mouseup
, mouseout
, error
or click
.
selector (string|function)
Any kind of valid CSS selector supported by matchesSelector
. Some selectors, like #id
or 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 (with eventData
as an optional third parameter) is equivalent to .on(eventType, null, handler[, eventData])
.
handler (function|*)
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.
eventData (*)
If defined and non-null, will be made available in event.data
.
Calling off
with no arguments will remove all registered listeners, effectively resetting the instance.
eventType (string)
Remove handlers for events matching this type considering the other parameters.
selector (string|function)
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)
(the third parameter will be ignored).
handler (function)
Only remove listeners registered with the given handler function, among the other arguments.
element (Node)
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. Enjoy.
FAQs
Create and manage a DOM event delegator.
The npm package dom-delegate receives a total of 2,849 weekly downloads. As such, dom-delegate popularity was classified as popular.
We found that dom-delegate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.