Browse Source

Added debounce functionality to remote validation

n4cer500 11 years ago
parent
commit
770183cf3a
4 changed files with 65 additions and 34 deletions
  1. 2 1
      demo/remote.html
  2. 31 16
      dist/js/bootstrapValidator.js
  3. 1 1
      dist/js/bootstrapValidator.min.js
  4. 31 16
      src/js/validator/remote.js

+ 2 - 1
demo/remote.html

@@ -110,7 +110,8 @@ $(document).ready(function() {
                     },
                     remote: {
                         url: 'remote.php',
-                        message: 'The username is not available'
+                        message: 'The username is not available',
+                        debounceDelay: 800
                     },
                     different: {
                         field: 'password',

+ 31 - 16
dist/js/bootstrapValidator.js

@@ -3137,7 +3137,9 @@
                 return true;
             }
 
-            var name = $field.attr('data-bv-field'), data = options.data;
+            var name = $field.attr('data-bv-field'),
+                data = options.data;
+
             if (data == null) {
                 data = {};
             }
@@ -3148,21 +3150,34 @@
             data[options.name || name] = value;
 
             var dfd = new $.Deferred();
-            var xhr = $.ajax({
-                type: 'POST',
-                url: options.url,
-                dataType: 'json',
-                data: data
-            });
-            xhr.then(function(response) {
-                dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
-            });
-
-            dfd.fail(function() {
-                xhr.abort();
-            });
-
-            return dfd;
+
+            if (options.debounceDelay) {
+                if($.fn.bootstrapValidator.timer) {
+                    clearTimeout($.fn.bootstrapValidator.timer);
+                }
+                $.fn.bootstrapValidator.timer = setTimeout(runCallback, options.debounceDelay);
+                return dfd;
+            }
+            else
+                return runCallback();
+
+            function runCallback() {
+                var xhr = $.ajax({
+                    type: 'POST',
+                    url: options.url,
+                    dataType: 'json',
+                    data: data
+                });
+                xhr.then(function (response) {
+                    dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
+                });
+
+                dfd.fail(function () {
+                    xhr.abort();
+                });
+
+                return dfd;
+            }
         }
     };
 }(window.jQuery));

File diff suppressed because it is too large
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 31 - 16
src/js/validator/remote.js

@@ -27,7 +27,9 @@
                 return true;
             }
 
-            var name = $field.attr('data-bv-field'), data = options.data;
+            var name = $field.attr('data-bv-field'),
+                data = options.data;
+
             if (data == null) {
                 data = {};
             }
@@ -38,21 +40,34 @@
             data[options.name || name] = value;
 
             var dfd = new $.Deferred();
-            var xhr = $.ajax({
-                type: 'POST',
-                url: options.url,
-                dataType: 'json',
-                data: data
-            });
-            xhr.then(function(response) {
-                dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
-            });
-
-            dfd.fail(function() {
-                xhr.abort();
-            });
-
-            return dfd;
+
+            if (options.debounceDelay) {
+                if($.fn.bootstrapValidator.timer) {
+                    clearTimeout($.fn.bootstrapValidator.timer);
+                }
+                $.fn.bootstrapValidator.timer = setTimeout(runCallback, options.debounceDelay);
+                return dfd;
+            }
+            else
+                return runCallback();
+
+            function runCallback() {
+                var xhr = $.ajax({
+                    type: 'POST',
+                    url: options.url,
+                    dataType: 'json',
+                    data: data
+                });
+                xhr.then(function (response) {
+                    dfd.resolve($field, 'remote', response.valid === true || response.valid === 'true');
+                });
+
+                dfd.fail(function () {
+                    xhr.abort();
+                });
+
+                return dfd;
+            }
         }
     };
 }(window.jQuery));