Skip to content

Instantly share code, notes, and snippets.

@ilterocal
Last active March 6, 2018 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilterocal/36e03f63b9b9b7ca41fd3e1019898a8d to your computer and use it in GitHub Desktop.
Save ilterocal/36e03f63b9b9b7ca41fd3e1019898a8d to your computer and use it in GitHub Desktop.
button-loading directive for Vue2
import Vue from "vue"
Vue.directive('button-loading', {
update: function (el, binding, vnode) {
//do nothing if value not changed
if (binding.oldValue === binding.value) {
return;
}
if (binding.value === true) {
if ($(el).find('.fa.fa-spinner.fa-spin').length === 0) {
$(el).prepend('<i class="fa fa-spinner fa-spin"></i>');
}
$(el).attr('disabled','disabled');
} else {
$(el).find('.fa.fa-spinner.fa-spin').remove();
$(el).removeAttr('disabled');
}
}
});
/*
* Usage: simply add v-button-loading as a directive to your button
* and bind a boolean value which should be true when you want the button to be
* disabled and show a font-awesome spinner
<button class="btn btn-primary" v-on:click="test" v-button-loading="busy">
* Vue part
data: () => ({
'busy': false
}),
methods: {
test: function() {
this.busy = true;
var self = this;
setTimeout(function() {
self.busy = false,
},2000)
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment