Created
December 18, 2018 11:33
-
-
Save adriandmitroca/1da0a8f3b9f12c9aba0f54a4f85debdf to your computer and use it in GitHub Desktop.
Debounce done right in Vue.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import debounce from 'lodash/debounce'; | |
export default { | |
data() { | |
return { | |
loading: false, | |
query: null, | |
}; | |
}, | |
watch: { | |
query() { | |
this.debouncedFetch(); | |
}, | |
}, | |
created() { | |
this.debouncedFetch = debounce(this.fetch, 500); | |
}, | |
mounted() { | |
this.fetch(); | |
}, | |
methods: { | |
async fetch() { | |
try { | |
this.loading = true; | |
const { data } = await window.axios.post(this.fetchUrl, { query: this.query }); | |
} catch (e) { | |
console.error(e); | |
} | |
this.loading = false; | |
}, | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment