Giter VIP home page Giter VIP logo

v-debounce's People

Contributors

neves avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

v-debounce's Issues

nuxt3 plugin

Add the below to /plugins/debounce-directive.js

Then include in nuxt.config.js:
plugins: ['~/plugins/debounce-directive.js']

Finally use as suggested (v-model.lazy):
<input v-model.lazy="sample" v-debounce="delay" />

// plugins/debounce-directive.js
function debounce(fn, delay) {
    let timeoutID = null;
    return function (...args) {
        clearTimeout(timeoutID);
        timeoutID = setTimeout(() => {
            fn.apply(this, args);
        }, delay);
    };
}

export default defineNuxtPlugin((nuxtApp) => {
    nuxtApp.vueApp.directive('debounce', {
        mounted(el, binding) {
            if (el.tagName.toLocaleUpperCase() !== 'INPUT') {
                console.warn('v-debounce directive is intended for use with <input> elements and v-model.lazy.');
                return;
            }

            const debounceFunction = debounce(() => {
                // Create and dispatch the 'change' event to trigger v-model.lazy update
                const event = new Event('change', { bubbles: true });
                el.dispatchEvent(event);
            }, parseInt(binding.value, 10) || 500); // Use the directive's value as the debounce delay

            el.addEventListener('input', (event) => {
                event.stopImmediatePropagation(); // Prevent immediate propagation to allow for debouncing

                // Call the debounce function which will emit the 'change' event after delay
                debounceFunction();
            }, true);
        },
        beforeUnmount(el) {
            // Clean up if necessary
            el.removeEventListener('input', el.oninput);
        }
    });
});

Cannot accomplish with input dom input event

Try this one, which doesn't work:
<input @input="method()" v-debounce="400" :value="someValue">
This one works:
<input @changed="method()" v-debounce="400" :value="someValue">

I can't use v-model since I'm using the value as prop.

Note: I need @input event since @changed is broken (invoked on many cases).

Input value gets overridden after mutation

Please consider the following example:

https://codesandbox.io/s/vdebounce-input-override-ej4nz

Greatly simplified from a large application using observables to fetch filtered data from an API.
I've mocked the API response with a random timeout. The behavior of the input is very similar to what I get in the app.

When the responses come in, the input loses a 1 or 2 letters if you're typing.
If I drop the v-debounce or the .lazy modifier, input works fine but there's no debounce.

Is there a fix for this or do I have to use a different debounce wrapper (i.e: lodash) inside the watcher and dump v-debounce altogether?

Thank you.


Note: I also created the v-debounce tag on StackOverflow.

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.