Autocomplete: Improved cache demo.

This commit is contained in:
Scott González 2010-08-27 12:47:19 -04:00
parent 16e93d5189
commit 4400632f53

View File

@ -15,21 +15,20 @@
</style>
<script type="text/javascript">
$(function() {
var cache = {};
var cache = {},
lastXhr;
$( "#birds" ).autocomplete({
minLength: 2,
source: function(request, response) {
if ( request.term in cache ) {
response( cache[ request.term ] );
var term = request.term;
if ( term in cache ) {
response( cache[ term ] );
return;
}
$.ajax({
url: "search.php",
dataType: "json",
data: request,
success: function( data ) {
cache[ request.term ] = data;
lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
cache[ term ] = data;
if ( xhr === lastXhr ) {
response( data );
}
});