Created
November 28, 2011 16:54
-
-
Save purcell/1401067 to your computer and use it in GitHub Desktop.
Cached autocompletion of wikipedia page titles with jquery ui autocomplete plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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