Debounce the package filter so search is more responsive

This commit is contained in:
Steve Purcell 2013-09-29 17:40:34 +01:00
parent 745f7b6bec
commit 9d7a19d624
2 changed files with 25 additions and 1 deletions

View file

@ -165,6 +165,30 @@
// Directives // Directives
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
app.directive("debounceModel", ["$timeout", function($timeout) {
return {
restrict: "A",
require: "ngModel",
scope: {
"ngModel": "=",
"debounceModel": "="
},
link: function (scope, element, attrs, ngModel) {
//jshint unused: false
var delay = 250;
var copyValue;
scope.$watch(function() {
return ngModel.$modelValue;
}, function (value) {
$timeout.cancel(copyValue);
copyValue = $timeout(function() {
scope.debounceModel = value;
}, delay);
});
}
};
}]);
app.directive("viewOrError", function($rootScope) { app.directive("viewOrError", function($rootScope) {
return { return {
template: '<div>' + template: '<div>' +

View file

@ -4,7 +4,7 @@
<small ng-show="totalDownloads">(<span ng-bind="totalDownloads"></span> downloads to date)</small> <small ng-show="totalDownloads">(<span ng-bind="totalDownloads"></span> downloads to date)</small>
</h2> </h2>
<p> <p>
<input type="text" ng-model="searchTerms" placeholder="Enter filter terms" autofocus> <input type="text" ng-model="enteredSearchTerms" debounce-model="searchTerms" placeholder="Enter filter terms" autofocus>
<span class="muted">&nbsp;<ng-pluralize when="{1: '1 match', 'other': '{} matches'}" count="(packages | filter:packageMatcher(searchTerms)).length"/></span> <span class="muted">&nbsp;<ng-pluralize when="{1: '1 match', 'other': '{} matches'}" count="(packages | filter:packageMatcher(searchTerms)).length"/></span>
</p> </p>
<table id="package-list" class="table table-bordered table-responsive table-hover"> <table id="package-list" class="table table-bordered table-responsive table-hover">