r3426 committed - autocomplete: proper caching implementation, needs the number of items...

r3426 committed - autocomplete: proper caching implementation, needs the number of items...


Revision: 3426
Author: joern.zaefferer
Date: Mon Nov 9 07:37:57 2009
Log: autocomplete: proper caching implementation, needs the number of items
provided from the source specified, need still to update spec (and disable
by default?)
http://code.google.com/p/jquery-ui/source/detail?r=3426
Modified:
/branches/dev/tests/unit/autocomplete/autocomplete_defaults.js
/branches/dev/tests/unit/autocomplete/autocomplete_options.js
/branches/dev/tests/visual/autocomplete/combobox.html
/branches/dev/tests/visual/autocomplete/default.html
/branches/dev/tests/visual/autocomplete/remote-jsonp.html
/branches/dev/tests/visual/autocomplete/remote.html
/branches/dev/ui/jquery.ui.autocomplete.js
=======================================
--- /branches/dev/tests/unit/autocomplete/autocomplete_defaults.js    Thu Nov
5 11:25:02 2009
+++ /branches/dev/tests/unit/autocomplete/autocomplete_defaults.js    Mon Nov
9 07:37:57 2009
@@ -3,7 +3,9 @@
*/
var autocomplete_defaults = {
-    cache: true,
+    cache: {
+        limit: 50
+    },
    delay: 300,
    disabled: false,
    minLength: 1,
=======================================
--- /branches/dev/tests/unit/autocomplete/autocomplete_options.js    Thu Nov
5 13:15:35 2009
+++ /branches/dev/tests/unit/autocomplete/autocomplete_options.js    Mon Nov
9 07:37:57 2009
@@ -34,9 +34,19 @@
    autocomplete.search("schi");
}
-test("cache: true", function() {
+test("cache: default", function() {
+    expect(2);
+    search($("#autocomplete").autocomplete({
+        source: source
+    }));
+});
+
+test("cache: {limit:4}", function() {
    expect(3);
    search($("#autocomplete").autocomplete({
+        cache: {
+            limit: 4
+        },
        source: source
    }));
});
=======================================
--- /branches/dev/tests/visual/autocomplete/combobox.html    Thu Nov 5
10:46:03 2009
+++ /branches/dev/tests/visual/autocomplete/combobox.html    Mon Nov 9
07:37:57 2009
@@ -31,6 +31,7 @@
                                };
                        });
                    },
+                    cache: false,
                    delay: 0,
                    change: function(e, ui) {
                        if (!ui.item) {
=======================================
--- /branches/dev/tests/visual/autocomplete/default.html    Thu Nov 5
10:46:03 2009
+++ /branches/dev/tests/visual/autocomplete/default.html    Mon Nov 9
07:37:57 2009
@@ -26,6 +26,7 @@
        $("#tags").autocomplete({
            source:
["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"],
+            cache: false,
            delay: 0,
            search: function() {
                log("Searching for: " + this.value);
=======================================
--- /branches/dev/tests/visual/autocomplete/remote-jsonp.html    Thu Nov 5
10:46:03 2009
+++ /branches/dev/tests/visual/autocomplete/remote-jsonp.html    Mon Nov 9
07:37:57 2009
@@ -27,6 +27,9 @@
        $("#city").autocomplete({
            source: function(request, response) {
                $.ajax({
+                    cache: {
+                        limit: 15
+                    },
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
=======================================
--- /branches/dev/tests/visual/autocomplete/remote.html    Thu Nov 5 10:46:03
2009
+++ /branches/dev/tests/visual/autocomplete/remote.html    Mon Nov 9 07:37:57
2009
@@ -25,6 +25,9 @@
        }
        $("#birds").autocomplete({
+            cache: {
+                limit: 13
+            },
            source: "search.php",
            minLength: 2,
            search: function() {
=======================================
--- /branches/dev/ui/jquery.ui.autocomplete.js    Thu Nov 5 13:21:23 2009
+++ /branches/dev/ui/jquery.ui.autocomplete.js    Mon Nov 9 07:37:57 2009
@@ -93,13 +93,13 @@
        } else {
            this.source = this.options.source;
        }
-        // experimental caching code
+        // proxy with cache provider
        if (this.options.cache) {
            var self = this;
            this.source = (function(source) {
                return function(request) {
                    var matcher = new
RegExp(request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|
\\])/gi, "\\$1"), "i");
-                    if (new RegExp(self.cache.term).test(request.term) &&
self.cache.shrinking && self.cache.content) {
+                    if (new RegExp(self.cache.term).test(request.term) &&
self.cache.content && self.cache.content.length < self.options.cache.limit)
{
                        // TODO refactor with array-source above
                        return $.grep(self.cache.content, function(value) {
                         return matcher.test(value.result)
@@ -124,11 +124,8 @@
            function response(content) {
                if (content.length) {
                    content = self.normalize(content);
-                    // experimental caching code
+                    // cache response
                    if (self.options.cache) {
-                        // TODO invalidate cache, eg. on delete or when nothing is matched(?)
-                        //if (self.cache.content && self.cache.content.length >
content.length)
-                            self.cache.shrinking = self.cache.content &&
self.cache.content.length > content.length;
                        self.cache.term = value;
                        self.cache.content = content;
                    }
@@ -156,6 +153,7 @@
            this.menu.element.remove();
            this.menu = null;
        }
+        // TODO don't trigger when input is below minLength
        if (this.previous != this.element.val()) {
            this._trigger("change", null, selected);
        }
@@ -237,7 +235,9 @@
$.ui.autocomplete.defaults = {
    // caching isn't working yet, disable by default for now
-    cache: false,
+    cache: {
+        limit: 50
+    },
    minLength: 1,
    delay: 300
}