Giter Site home page Giter Site logo

Comments (7)

ryanmorr avatar ryanmorr commented on June 14, 2024 1

Flexibility is what I had in mind. I like the idea of adding both the once and off functions rather than one or the other.

I think i'll start working on it soon. Appreciate the input!

from ready.

ryanmorr avatar ryanmorr commented on June 14, 2024

As it stands now, no there is not a way to disconnect the ready listener, sorry.

I would suggest using a class name or data attribute unique to the element(s) that you are observing. Once the listener is dispatched for that particular selector, just ensure no other elements matching that selector are added to the DOM. That should give you the result you're looking for.

I will, however, leave this issue open as I consider adding this functionality.

from ready.

bengrosser avatar bengrosser commented on June 14, 2024

Great, the unique ID was my thought too, though I wasn't sure whether there might be a perceptible cost to having an increasing number of open ready()'sβ€”even if I know nothing else will emerge in the DOM to trigger them.

Another way to think about it would be to have a flag that makes a ready a one use only (e.g. finds first instance of the element, triggers the callback, and disconnects).

from ready.

ryanmorr avatar ryanmorr commented on June 14, 2024

I wouldn't worry about performance, only one mutation observer is used no matter how many times you open a ready listener. Although it's not ideal to have the mutation observer open when no more ready callbacks are expected.

I don't mind your suggestion, maybe something like ready.once for one offs would be an easy solution? I was also thinking ready.off('.foo') to explicitly remove a listener for a particular selector might be a good idea?

from ready.

bengrosser avatar bengrosser commented on June 14, 2024

I see on the single observer; very nice. ready.off('foo') is probably more flexible, as it allows 1-n uses?

from ready.

bengrosser avatar bengrosser commented on June 14, 2024

Of course! Thank you for the code. I had my own clunky MutationObserver going, but you made it elegant and simple to use.

from ready.

ryanmorr avatar ryanmorr commented on June 14, 2024

Made the change, it's not quite what we discussed here, instead I opted for a simpler solution. Now, the ready function returns a function that you can call to remove the listener. Refer to the updated readme for an example.

Thanks again.

from ready.

Related Issues (6)

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.