Properly debounce searching and make sure to only show results matching the current query.
This commit is contained in:
parent
48f3e9af1d
commit
5ae2975134
5 changed files with 70 additions and 3 deletions
66
static/lib/angular-debounce.js
vendored
Normal file
66
static/lib/angular-debounce.js
vendored
Normal file
|
@ -0,0 +1,66 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('debounce', [])
|
||||
.service('debounce', ['$timeout', function ($timeout) {
|
||||
return function (func, wait, immediate) {
|
||||
var timeout, args, context, result;
|
||||
function debounce() {
|
||||
/* jshint validthis:true */
|
||||
context = this;
|
||||
args = arguments;
|
||||
var later = function () {
|
||||
timeout = null;
|
||||
if (!immediate) {
|
||||
result = func.apply(context, args);
|
||||
}
|
||||
};
|
||||
var callNow = immediate && !timeout;
|
||||
if (timeout) {
|
||||
$timeout.cancel(timeout);
|
||||
}
|
||||
timeout = $timeout(later, wait);
|
||||
if (callNow) {
|
||||
result = func.apply(context, args);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
debounce.cancel = function () {
|
||||
$timeout.cancel(timeout);
|
||||
timeout = null;
|
||||
};
|
||||
return debounce;
|
||||
};
|
||||
}])
|
||||
.directive('debounce', ['debounce', '$parse', function (debounce, $parse) {
|
||||
return {
|
||||
require: 'ngModel',
|
||||
priority: 999,
|
||||
link: function ($scope, $element, $attrs, ngModelController) {
|
||||
var debounceDuration = $parse($attrs.debounce)($scope);
|
||||
var immediate = !!$parse($attrs.immediate)($scope);
|
||||
var debouncedValue, pass;
|
||||
var prevRender = ngModelController.$render.bind(ngModelController);
|
||||
var commitSoon = debounce(function (viewValue) {
|
||||
pass = true;
|
||||
ngModelController.$setViewValue(viewValue);
|
||||
pass = false;
|
||||
}, parseInt(debounceDuration, 10), immediate);
|
||||
ngModelController.$render = function () {
|
||||
prevRender();
|
||||
commitSoon.cancel();
|
||||
//we must be first parser for this to work properly,
|
||||
//so we have priority 999 so that we unshift into parsers last
|
||||
debouncedValue = this.$viewValue;
|
||||
};
|
||||
ngModelController.$parsers.unshift(function (value) {
|
||||
if (pass) {
|
||||
debouncedValue = value;
|
||||
return value;
|
||||
} else {
|
||||
commitSoon(ngModelController.$viewValue);
|
||||
return debouncedValue;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
}]);
|
Reference in a new issue