Skip to content

Instantly share code, notes, and snippets.

@purcell
Created November 28, 2011 16:54
Show Gist options
  • Save purcell/1401067 to your computer and use it in GitHub Desktop.
Save purcell/1401067 to your computer and use it in GitHub Desktop.
Cached autocompletion of wikipedia page titles with jquery ui autocomplete plugin
function cached_completer(completer) {
var cache = {};
return function(request, response) {
if (request.term in cache) {
response(cache[request.term]);
} else {
completer(request, function(resp) {
cache[request.term] = resp;
response(resp);
});
}
};
}
var completers = {
wikipedia_page_titles: cached_completer(function(request, response) {
$.ajax({
url: "http://en.wikipedia.org/w/api.php",
data: {
action: "opensearch",
search: request.term,
format: "json"
},
dataType: "jsonp",
success: function(data) {
response(data[1]);
}
});
})
};
function autocompleteReady() {
$(".wikipedia-name-search").autocomplete({
minLength: 3,
delay: 500,
source: completers.wikipedia_page_titles});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment