Updated the typeahead directive to cache queries.

This commit is contained in:
Tanshu 2013-05-18 15:03:50 +05:30
parent 572a630520
commit fa95e79e0f
2 changed files with 23 additions and 15 deletions

View File

@ -1,6 +1,6 @@
CACHE MANIFEST CACHE MANIFEST
# version 2013-05-18.1 # version 2013-05-18.2
CACHE: CACHE:
/partial/404.html /partial/404.html

View File

@ -5,13 +5,18 @@ overlord_directive.directive('ngAutocomplete', ['$q', '$parse', 'DeepLabel', fun
return { return {
restrict: 'A', restrict: 'A',
link: function (scope, element, attrs, ngModel) { link: function (scope, element, attrs, ngModel) {
var labels = [], var labelsCache = {},
mappedCache = {},
labels = [],
mapped = {}, mapped = {},
deepLabel = DeepLabel; deepLabel = DeepLabel;
element.typeahead({ element.typeahead({
source: function (query, process) { source: function (query, process) {
if (query in labelsCache) {
labels = labelsCache[query];
mapped = mappedCache[query];
process(labels);
} else {
var Entity = angular.injector(['overlord.service']).get(attrs.resource); var Entity = angular.injector(['overlord.service']).get(attrs.resource);
Entity.autocomplete({term: query, count: 20}, function (result) { Entity.autocomplete({term: query, count: 20}, function (result) {
labels = []; labels = [];
@ -21,8 +26,11 @@ overlord_directive.directive('ngAutocomplete', ['$q', '$parse', 'DeepLabel', fun
mapped[label] = item mapped[label] = item
labels.push(label) labels.push(label)
}) })
labelsCache[query] = labels;
mappedCache[query] = mapped;
process(labels) process(labels)
}); });
}
}, },
minLength: 1, minLength: 1,
items: 20, items: 20,