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

Transferrable Events #2

Closed
shama opened this issue Jun 21, 2016 · 7 comments
Closed

Transferrable Events #2

shama opened this issue Jun 21, 2016 · 7 comments

Comments

@shama
Copy link
Owner

shama commented Jun 21, 2016

Currently it fires events based on if the element registered matches. Element references can get lost, fairly easily in complex DOM changes.

So instead, what if we generate a unique id, set with el.dataset['onloadid'] = uniqid then match elements based on that id. This way, it doesn't matter what happens to the element reference, if it has a matching event id the event is triggered.

/cc @yoshuawuyts @freeman-lab

@yoshuawuyts
Copy link
Collaborator

This sounds perfect ❤️

@timwis
Copy link
Contributor

timwis commented Jun 23, 2016

Sounds great to me, though I'll admit it's a bit over my head. Reading it, though, reminded me of two things: (1) this comment in morphdom, and (2) fairly certain throw-down uses unique IDs on its elements as well. Perhaps both those points are irrelevant, but thought I'd share them just in case. Thanks for working on this, @shama.

@shama
Copy link
Owner Author

shama commented Jun 23, 2016

@timwis Thanks! The idea is this:

  • When you call onload(el) on an element, it generates a pointer to the onload/unload functions then sets it as a data-onloadid attribute on the element.
  • That attribute travels around with the element as the DOM is manipulated.
  • Any time an element is added/removed, the MutationObserver checks for a data-onloadid attribute.
  • If it has one, we find the matching pointer and call the appropriate onload or unload.

I have the basic idea implemented here: https://github.com/shama/on-load/blob/transferrable-events/index.js and it seems to work great so far. I have some more optimization ideas to try though.

@shama
Copy link
Owner Author

shama commented Jun 23, 2016

Published as 2.1.0. Give it a go!

It simplified shama/modal-element@8f54783 quite a bit. :D

@shama shama closed this as completed Jun 23, 2016
@freeman-lab
Copy link

this is awesome @shama !

@yoshuawuyts
Copy link
Collaborator

w00t! 🎉

@timwis
Copy link
Contributor

timwis commented Jun 23, 2016

Woah, you're done already?! Nice! Thanks for breaking it down. Just threw it into my choo app in 30 seconds and it seems to be working great.

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

No branches or pull requests

4 participants