Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow instances to handle more than one element #27

Closed
swang opened this issue Mar 8, 2018 · 3 comments
Closed

Allow instances to handle more than one element #27

swang opened this issue Mar 8, 2018 · 3 comments

Comments

@swang
Copy link
Contributor

swang commented Mar 8, 2018

Hi. Just seeing if you're interesting in adding this before I implement it.

I want to have one instance of Draggable handle all elements that match an element. Easiest example is I have a chessboard and want to be able to drag all the pieces on the board without having to declare 32 instances of Draggable. E.g. I want to specify that I want all (<div class="piece"></div>) that are children of a parent element (<div class="board"></div>) be draggable.

The way I'm thinking of implementing this is to specify a new parentEle parameter, and make element accept a string. Then only need an event listener attached to parentEle instead of for each element. I'm not dead-set on names/functionality so if you are interested, would love to hear your feedback about how the API should work for this.

I know this library wants to support IE8, and it seems that querySelectorAll actually works in IE8, but only if its in standards mode[0]. I don't think polyfilling querySelectorAll would be worth it just to support IE8 quirks/compat modes. So implementing this for IE8 standards-only mode is what I would target. Then I guess if users need to support IE8 in non-standards mode they could just polyfill querySelectorAll.

Obviously the original methods would stay the same way in terms of supporting IE8, the requirement would only be for this new functionality.

If you don't think this library's core should support multiple elements, or if features need to support all modes of IE8 then I'll just fork the library. But I thought I'd see if there was interest in adding this to the main library (with the IE8 caveats) before rewriting it for myself.

[0] http://web.archive.org/web/20171004213917/https://msdn.microsoft.com/en-us/library/ff462057.aspx#selectorapi (The Selectors API is defined as part of the Selectors API specification and is only available to Web pages displayed in IE8 standards mode)

@bcherny
Copy link
Owner

bcherny commented Mar 8, 2018

Hi @swang! Can you talk a little about the motivation for this? Why would you want a single event listener instead of one for each draggable?

@swang
Copy link
Contributor Author

swang commented Mar 9, 2018

My use case would be multiple draggable elements sitting under the same container. E.g. Being able to move pieces on a chess board.

Generally speaking delegating events to the container (a parent element) should use less memory than assigning events to each individual element. I'm assuming it's also quicker to instantiate that one event rather than multiple events. I've tried this w/ click events, but may also have to test mousemove events to see if there is still the same improvement.

So if I have a page that has several different chessboards that show different scenarios. Instead of having to create an event for every piece of every board. I can just define one listen event delegator to handle it.

Doing this also allows me to add/remove draggable elements without having to redefine a new draggable instance each time an element is created since the event delegate handles it.

@bcherny
Copy link
Owner

bcherny commented Mar 10, 2018

Generally speaking delegating events to the container (a parent element) should use less memory than assigning events to each individual element.

This is not really the case with modern browsers, and hasn't been for some time. If you run a benchmark, you'll see there is no difference between the delegated and non-delegated versions.

I'm assuming it's also quicker to instantiate that one event rather than multiple events

Maybe by, like, nanoseconds? Have you tried profiling this to get some real numbers?

This doesn't seem like a good use case to me. If you can produce a benchmark that shows an improvement with delegation, I'd be more than happy to support this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants