Comments (12)
Tough one, we wanted to match the browser's native API for add/removeEventListener for useCapture
, which is why the boolean was chosen.
For comparison the native browser APIs use these arguments:-
target.addEventListener(type, listener[, useCapture]);
target.removeEventListener(type, listener[, useCapture]);
Our API uses these arguments:-
target.on(type[, selector], listener[, useCapture]);
target.off(type[, selector], listener[, useCapture]);
In any other case I would passionately agree with you and much prefer passing options objects around.
Do you think is a reasonable justification for bending the boolean trap rule?
from ftdomdelegate.
(Oops - made a slight typo in the APIs - updated now)
from ftdomdelegate.
In that situation, a compromise would be to still accept a Boolean for compatibility reason, but also allowing an configuration object. It incurs a small overhead, but then the latter can be the one recommended in the documentation and code snippets.
from ftdomdelegate.
Something like this?
dd.on({
type: /* string */,
selector: /* string !optional */,
listener: /* function */,
useCapture: /* boolean !optional */
});
dd.on(type[, selector], listener[, useCapture]);
I also wonder whether we should support addEventListener
and removeEventListener
.
from ftdomdelegate.
I'd be happy with any of these options:
- keeping API as is
- providing aliases for
addEventListener
andremoveEventListener
foron
andoff
. - providing an alternate object based API
- both 2 and 3.
Pinging past and present contributors: @wilsonpage @wheresrhys @georgecrawford @orangemug and @mattcg for their views and I'll go along with that.
from ftdomdelegate.
Even simpler:
delegate.on('click', clickHandler, { useCapture: true });
from ftdomdelegate.
I thought about that but it's kinda ugly in the selector case (which is also the most common):-
delegate.on('click', clickHandler, { selector: '.my-btn', useCapture: true });
// or
delegate.on('click'[, selector], clickHandler, { useCapture: true });
from ftdomdelegate.
Also it's worth saying the useCapture
option is used only in very extreme cases. I've only ever needed it once - when building an extension of FastClick called Selective FastClick* (which I think is safe to say a fairly extreme example of usage of DOM events) - the rest of the time the default value FT DOM Delegate picks is 'right' for all the applications and tools I've been involved with building.
* https://github.com/matthew-andrews/selective-fastclick/blob/master/index.js#L39
from ftdomdelegate.
For what it's worth, I think you can satisfy all these comments with:
// options (object):
// useCapture (boolean) - defaults to 'sensible' value for the eventType
delegate.on(eventType[, selector], clickHandler[, options]);
If you want to further embrace the existing EventTarget
API, you can add:
target.addEventListener(type, listener[, useCapture]);
//...proxies to:
delegate.on(type, listener, {useCapture: useCapture});
from ftdomdelegate.
OK for now let's go with @georgecrawford and @ariya's suggestion of
delegate.on(type, listener, {useCapture: useCapture});
... and save addEventListener
/removeEventListener
for a rainy day.
from ftdomdelegate.
Isn't the norm to have the function at the end because its a bit more readable for inline usage. So the def would be
delegate.on(eventType, selector, options, clickHandler);
So basically this
delegate.on("click", ".menu", {useCapture: true}, function() {
// ...
});
Rather than this
delegate.on("click", ".menu", function() {
// ...
}, {useCapture: true});
from ftdomdelegate.
useCapture
is such a little used, advanced-usage-only part of the API of a library who's API is very, very small I'm really struggling to justify the extra complexity here...
ftdomdelegate
tries to be as close to the browser APIs as possible - merely providing event delegation - and its the underlying browser native APIs, the ones we're mimicking that have the boolean trap (and in the case of Gecko it's a bad one at that, true, false).
target.addEventListener(type, listener[, useCapture]);
target.addEventListener(type, listener[, useCapture, wantsUntrusted ]); // Gecko/Mozilla only
https://developer.mozilla.org/en/docs/Web/API/EventTarget.addEventListener
Thank you for your thoughts, suggestions and consideration but I am increasingly thinking that this boolean trap should be fixed in the browser itself - and then after that we can fix it here by matching that.
from ftdomdelegate.
Related Issues (20)
- Event namespaces: Interested in including them? HOT 7
- Behavior is non-standard when handler returns false HOT 4
- stopPropagation does not stop propagation to listeners added with ftdomdelegate
- Event listener execution order is backwards when useCapture = true HOT 1
- Browser compatible bower build HOT 6
- unexpected mouseover/mouseout behavior w/ capture
- multiple event binding HOT 3
- TypeError: 'undefined' is not a function (evaluating 'Delegate.prototype.handle.bind(this)') in Konacha HOT 5
- Waiting for window load event in README examples HOT 2
- Error on uglification HOT 2
- Click events are fired on disabled button with element like i tag. HOT 3
- useCapture not properly documented HOT 1
- Inconsistently named installs with bower HOT 6
- Custom events HOT 1
- Change requirements for IE8 support
- Why are there two names for this module? HOT 5
- IE10 click event doesn't work on an SVG. HOT 3
- Move to CircleCI and use occ to publish to npm on tag HOT 1
- ftdomdelegate complete breaking checklist HOT 1
- IE11 SCRIPT5007: Unable to get property 'call' of undefined or null reference
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ftdomdelegate.