Skip to content

Instantly share code, notes, and snippets.

@joallard
Created July 4, 2021 23:06
Show Gist options
  • Save joallard/5dba6efeb6ddeac1c1323ea490e7af63 to your computer and use it in GitHub Desktop.
Save joallard/5dba6efeb6ddeac1c1323ea490e7af63 to your computer and use it in GitHub Desktop.
Vue Request Indicator
<template lang="pug">
.request-indicator
.sending(v-if="inProgress")
.response-message(v-else)
</template>
<script>
const http = window.fetch
export default {
props: ["url", "method"],
data(){
inProgress: false,
response: {},
message: "",
},
methods: {
send({data = {}, headers = {}}){
let {url, method} = this
try {
this.inProgress = true
let result = http(url, {method, data, headers})
.then(this.onReceive)
} finally {
this.inProgress = false
}
return result
},
onReceive(response){
this.response = response
let {message, errors, result} = response
if(errors) this.$emit("error", errors);
if(result) this.$emit("success", result);
this.$emit("complete")
},
}
computed: {
},
}
</script>
<style lang="sass">
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment