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