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,24 +5,32 @@ 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) {
var Entity = angular.injector(['overlord.service']).get(attrs.resource); if (query in labelsCache) {
Entity.autocomplete({term: query, count: 20}, function (result) { labels = labelsCache[query];
labels = []; mapped = mappedCache[query];
mapped = {}; process(labels);
$.each(result, function (i, item) { } else {
var label = deepLabel(item, attrs.label); var Entity = angular.injector(['overlord.service']).get(attrs.resource);
mapped[label] = item Entity.autocomplete({term: query, count: 20}, function (result) {
labels.push(label) labels = [];
}) mapped = {};
process(labels) $.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, minLength: 1,
items: 20, items: 20,