]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete: Improved cache demo.
authorScott González <scott.gonzalez@gmail.com>
Fri, 27 Aug 2010 16:47:19 +0000 (12:47 -0400)
committerScott González <scott.gonzalez@gmail.com>
Fri, 27 Aug 2010 16:47:19 +0000 (12:47 -0400)
demos/autocomplete/remote-with-cache.html

index bd261f44ee5ec42e4363e1915f21c5a62b422f82..77741fbe916c1c93e09eec32a3839b5473960a2b 100644 (file)
        </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 );
                                        }
                                });