aboutsummaryrefslogtreecommitdiffstats
path: root/demos/autocomplete/remote-with-cache.html
diff options
context:
space:
mode:
Diffstat (limited to 'demos/autocomplete/remote-with-cache.html')
-rw-r--r--demos/autocomplete/remote-with-cache.html78
1 files changed, 31 insertions, 47 deletions
diff --git a/demos/autocomplete/remote-with-cache.html b/demos/autocomplete/remote-with-cache.html
index a00c741e4..a6b02f255 100644
--- a/demos/autocomplete/remote-with-cache.html
+++ b/demos/autocomplete/remote-with-cache.html
@@ -1,47 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
- <meta charset="UTF-8" />
- <title>jQuery UI Autocomplete Remote with caching demo</title>
- <link type="text/css" href="../../themes/base/jquery.ui.all.css" rel="stylesheet" />
- <script type="text/javascript" src="../../jquery-1.4.2.js"></script>
- <script type="text/javascript" src="../../ui/jquery.ui.core.js"></script>
- <script type="text/javascript" src="../../ui/jquery.ui.widget.js"></script>
- <script type="text/javascript" src="../../ui/jquery.ui.position.js"></script>
- <script type="text/javascript" src="../../ui/jquery.ui.autocomplete.js"></script>
- <link type="text/css" href="../demos.css" rel="stylesheet" />
- <script type="text/javascript">
+ <meta charset="utf-8">
+ <title>jQuery UI Autocomplete - Remote with caching</title>
+ <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
+ <script src="../../jquery-1.4.4.js"></script>
+ <script src="../../ui/jquery.ui.core.js"></script>
+ <script src="../../ui/jquery.ui.widget.js"></script>
+ <script src="../../ui/jquery.ui.position.js"></script>
+ <script src="../../ui/jquery.ui.menu.js"></script>
+ <script src="../../ui/jquery.ui.autocomplete.js"></script>
+ <link rel="stylesheet" href="../demos.css">
+ <style>
+ .ui-autocomplete-loading { background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat; }
+ </style>
+ <script>
$(function() {
- function log(message) {
- $("<div/>").text(message).prependTo("#log");
- $("#log").attr("scrollTop", 0);
- }
-
- var cache = {};
- $("#birds").autocomplete({
- source: function(request, response) {
- if (cache.term == request.term && cache.content) {
- response(cache.content);
- return;
- }
- if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
- response($.ui.autocomplete.filter(cache.content, request.term));
+ var cache = {},
+ lastXhr;
+ $( "#birds" ).autocomplete({
+ minLength: 2,
+ source: function( request, response ) {
+ var term = request.term;
+ if ( term in cache ) {
+ response( cache[ term ] );
return;
}
- $.ajax({
- url: "search.php",
- dataType: "json",
- data: request,
- success: function(data) {
- cache.term = request.term;
- cache.content = data;
- response(data);
+
+ lastXhr = $.getJSON( "search.php", request, function( data, status, xhr ) {
+ cache[ term ] = data;
+ if ( xhr === lastXhr ) {
+ response( data );
}
});
- },
- minLength: 2,
- select: function(event, ui) {
- log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
}
});
});
@@ -56,20 +47,13 @@
<input id="birds" />
</div>
-<div class="ui-widget" style="margin-top:2em; font-family:Arial">
- Result:
- <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
-</div>
-
</div><!-- End demo -->
+
+
<div class="demo-description">
-<p>
-The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are bird names, displayed when at least two characters are entered into the field.
-</p>
-<p>
-Similar to the remote datasource demo, though this adds some local caching to improve performance. The cache here saves just one query, and could be extended to cache multiple values, one for each term.
-</p>
+<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are bird names, displayed when at least two characters are entered into the field.</p>
+<p>Similar to the remote datasource demo, though this adds some local caching to improve performance. The cache here saves just one query, and could be extended to cache multiple values, one for each term.</p>
</div><!-- End demo-description -->
</body>