Autocomplete more detailed caching

Autocomplete more detailed caching


Sorry if this is in the wrong place but I'm a newbie to the group.
First of I think the autocomplete plugin from Jörn Zaefferer is great.
However, one limitation to the caching model with the plugin that I
have found is that it only uses the values in the cache.
One place I use the plugin is for a destination dropdown
e.g. Format - Data[0] (Data[1])
Egypt (North and Central Africa)
Alexandria (Egypt)
Sharm El Sheikh (Egypt)
Luxor (Egypt)
Hurghada (Egypt)
Cairo (Egypt)
Aswan (Egypt)
If someone begins to type Egypt I want all these to return. However
once the first response has been cached only the first entry - Egypt
(North and Central Africa) will be returned. Also I don't want to use
matchContains as I only want the 'Egypts' returned.
To cut a long story short I added the following functionality which I
would really appreciate being considered.
-- SNIP--
$.Autocompleter.defaults = {
-- SNIP--
additionalCacheIndex: null
};
-- SNIP --
$.Autocompleter.Cache = function(options) {
var data = {};
var length = 0;
var indexes = (options.additionalCacheIndex) ? $.makeArray
(options.additionalCacheIndex) : null;
-- SNIP --
return {
flush: flush,
add: add,
populate: populate,
load: function(q) {
if (!options.cacheLength || !length)
return null;
/*
* if dealing w/local data and matchContains than we
must make sure
* to loop through all the data collections looking for
matches
*/
if (!options.url && options.matchContains) {
// track all matches
var csub = [];
// loop through all the data grids for matches
for (var k in data) {
// don't search through the stMatchSets[""]
(minChars: 0) cache
// this prevents duplicates
if (k.length > 0) {
var c = data[k];
$.each(c, function(i, x) {
// if we've got a match, add it to the
array
if (matchSubset(x.value, q)) {
csub.push(x);
}
});
}
}
return csub;
} else
// if the exact item exists, use it
if (data[q]) {
return data[q];
} else
if (options.matchSubset) {
for (var i = q.length - 1; i >= options.minChars;
i--) {
var c = data[q.substr(0, i)];
if (c) {
var csub = [];
$.each(c, function(i, x) {
if (matchSubset(x.value, q)) {
csub[csub.length] = x;
}
// if the are additional index try
them
else if (indexes && indexes.length >
0) {
$.each(indexes, function(index, y)
{
if (y < x.data.length &&
matchSubset(x.data[y], q)) csub[csub.length] = x;
});
}
});
return csub;
}
}
}
return null;
}
};
};
Now I can also match on data index 1 etc....