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
# version 2013-05-18.1
# version 2013-05-18.2
CACHE:
/partial/404.html

View File

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