Git Product home page Git Product logo

Comments (12)

matthew-andrews avatar matthew-andrews commented on July 26, 2024

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.

matthew-andrews avatar matthew-andrews commented on July 26, 2024

(Oops - made a slight typo in the APIs - updated now)

from ftdomdelegate.

ariya avatar ariya commented on July 26, 2024

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.

matthew-andrews avatar matthew-andrews commented on July 26, 2024

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.

matthew-andrews avatar matthew-andrews commented on July 26, 2024

I'd be happy with any of these options:

  1. keeping API as is
  2. providing aliases for addEventListener and removeEventListener for on and off.
  3. providing an alternate object based API
  4. 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.

ariya avatar ariya commented on July 26, 2024

Even simpler:

delegate.on('click', clickHandler, { useCapture: true }); 

from ftdomdelegate.

matthew-andrews avatar matthew-andrews commented on July 26, 2024

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.

matthew-andrews avatar matthew-andrews commented on July 26, 2024

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.

georgecrawford avatar georgecrawford commented on July 26, 2024

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.

matthew-andrews avatar matthew-andrews commented on July 26, 2024

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.

orangemug avatar orangemug commented on July 26, 2024

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.

matthew-andrews avatar matthew-andrews commented on July 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.