Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

Commit

Permalink
Better timers
Browse files Browse the repository at this point in the history
  • Loading branch information
ovanschie committed Mar 13, 2017
1 parent a100b65 commit b6ed8b7
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/v-validate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var validate = {},
errorBag = 'invalidFields',
validateTimer = null;
timers = {};

// exposed global options
validate.config = {};
Expand Down Expand Up @@ -83,19 +83,23 @@ validate.install = function (Vue) {
// Bind the v-model to
// the directive
if (this.params.vModel) {
this.identifier = this.params.vModel.replace(/\./g, '_');
this.expression = this.params.vModel;
} else {
this.expression = null;
console.error('No v-model on input');
}
},

update: function (value) {
clearTimeout(validateTimer);
if (typeof timers[this.identifier] != 'undefined') {
clearTimeout(timers[this.identifier]);
}

var vm = this;

Vue.nextTick(function () {
vm.validate(value);
})
});
},

validate: function (value) {
Expand All @@ -121,10 +125,8 @@ validate.install = function (Vue) {
}
}.bind(this));

var bagEntry = this.expression.replace(/\./g, '_');

if (typeof this.vm[errorBag] != "undefined") {
Vue.delete(this.vm[errorBag], bagEntry);
Vue.delete(this.vm[errorBag], this.identifier);
}

if (valid) {
Expand All @@ -133,7 +135,7 @@ validate.install = function (Vue) {
this.el.classList.remove('invalid');
} else {
// add the field to the errorbag
this.vm.$set(errorBag + '.' + bagEntry, true);
this.vm.$set(errorBag + '.' + this.identifier, true);

// instantly remove the valid class
this.el.classList.remove('valid');
Expand All @@ -144,9 +146,9 @@ validate.install = function (Vue) {
var vm = this;

// give a sec before adding the invalid class
validateTimer = setTimeout(function(){
timers[this.identifier] = setTimeout(function(){
vm.el.classList.add('invalid');
}, 1000);
}, 800);
}
}

Expand Down

0 comments on commit b6ed8b7

Please sign in to comment.