Skip to content

Instantly share code, notes, and snippets.

@preshetin
Last active May 22, 2017 20:07
Show Gist options
  • Save preshetin/b6687a6424e5c76258211f076f717042 to your computer and use it in GitHub Desktop.
Save preshetin/b6687a6424e5c76258211f076f717042 to your computer and use it in GitHub Desktop.
VueJS Status Component Example
<template>
<a v-if="isToggable" :class="getClass" @click.prevent="toggle">
{{ status }}
<i v-if="loading" class="fa fa-spinner fa-spin icon-resize-small"></i>
</a>
<span v-else :class="getClass">{{ status }}</span>
</template>
<script>
export default {
data() {
return {
status: '',
loading: false
}
},
mounted() {
this.status = this.value
},
computed: {
getClass() {
const classValue = 'label ';
const statusClass = {
new: "bg-green",
done: "bg-gray",
visible: "bg-blue",
invisible: "bg-gray",
pending: "bg-yellow",
success: "bg-green",
active: "bg-green",
inactive: "bg-red"
};
return classValue + statusClass[this.status];
},
isToggable() {
return typeof this.toggleUrl !== 'undefined'
}
},
methods: {
toggle() {
this.loading = true;
axios.get(this.toggleUrl).then(response => {
this.status = response.data;
this.loading = false;
})
}
},
props: ['value', 'toggleUrl']
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment