views:

2464

answers:

4

I am using jQuery.autocomplete(1.02) on my search box and I want exact string and substring matching. I don't care (yet!) about the database load, I'm happy for it to fire off a query every keystroke and bypass the caching entirely - I just don't want anything missed.

To this end I have tried setting cacheLength=1, the minimum permitted, but the autocomplete function refuses to fire off a GET request for each key up.

searchbox          GET_request

   'a'       ->    http://localhost/service_search_request?q=a
   'ar'      ->    http://localhost/service_search_request?q=ar
   'ars'     ->    http://localhost/service_search_request?q=ars

Instead, it sends the first and the third and misses the second, giving me the wrong results for 'ar' :-/ I've cleared my cache and sessions but it looks like some sort of caching is still going on. AFAIK I have no proxying going on and I'm shift-refreshing each time. It looks likely then that this behavior is from jQuery.autocomplete itself.

So my questions are...

A) Does this seem likely? i.e. is it a feature, or maybe a bug?

B) If so is there a clean way around it?...

C) If not, what autocomplete would you use instead?

Naturally D) No you're just using it incorrectly you douche! is always a possibility, and indeed the one I'd prefer having spent time going down this road - assuming it comes with a link to the docs I've failed to find / read!

Cheers,

Roger :)

+4  A: 

I wonder why cacheLength doesn't work, but had trouble with autocomplete too. IMHO, there are errors in it. However, in the list of options, there is a matchSubset you could set to false.

EDIT: somewhere around line 335 is a function called "request". You could add some debug messages to it, to see what happens: (note: you need firebug installed or "console" will be unknown)

function request(term, success, failure) {

 console.debug("ac request...");

 if (!options.matchCase)
  term = term.toLowerCase();

 var data = cache.load(term);

 console.debug("ac request 1, loaded data from cache: " + data + " term: " + term);

 // recieve the cached data
 if (data && data.length) {
  success(term, data);
 // if an AJAX url has been supplied, try loading the data now
 } else if( (typeof options.url == "string") && (options.url.length > 0) ){

  console.debug("ac request 2, data is not in the cache, request it");

"flushCache" can easily be used in the function you can attach / set as options. I used this, to clear the Cache, if there could be more data in the backend:

formatItem: function (data,i,n,value){
    if(i === (this.max -1)){
        console.debug("flushCache");
        jQuery(this).flushCache();
    }

    return data[1] + " (" + data[0] + ")";
}
Tim Büthe
Yeah tried matchSubset set to both true and false - nada :-/Dunno whether to have a hack at the autocomplete code or to look for another plugin. Not sure my javascript is hot enough to alter/fix it properly though and if I create my own hacky fork I'm lumbered with maintaining it, hmm.
I answered in the posting, take a look....
Tim Büthe
Thanks, sadly I'm still not quite clear on where to hook .flushCache into all this to make it either...1) preceed every keystrokeor2) have autocomplete call it every kbd eventI suppose I could attach it to a key down event but I feel sure there must be less sloppy ways I am ignorant of :-/
Thanks very much for your help BTW, if flushCache turns out to be a dead end I'll go grab the unminified version and try debugging as you suggested.
Tim Büthe
Problem solved - and whaddya know it was my sloppy javascript! Did a debug like you suggested and found out it was using default parameters for Caching and everything else. Turns out I was passing it the parameters incorrectly i.e. not{in:curly, brackets:like:, it:wanted} DOH! Thanx 4 all your help!
That’s good news! Didn't gave this JavaScript errors or did you just not see them until you installed Firebug?
Tim Büthe
A: 

I am having the same problem. Caching doesn't work although I have set the option cacheLength to 1.

With your solution to call the flushCache function after each printed term it works. I couldn't use the:

if(i === (this.max -1)){

since 'i' was e.g 1 after filtering but 'this.max' still 25 as the original backend query resulted in 25 returned rows.

However, this bug ONLY appears when typing words that contain the swedish characters 'å', 'ä' or 'ö'. So maybe the cashing works as expected but not with these special characters.

Anyway. the solution for me was to always call the flushCache control in the formatItem() function:

function formatItem(row, position, n, term) {

    if($("#keywords-h").length > 0){
     $("#keywords-h").flushCache();
        }

        // format Item
        return "<span>" + row[0] + "</span>";
}

Hope this helps someone and if someone is having the same problems with special characters please post a reply.

cl2csand
Update: The problem is partly solved. I made a mistake and hadn't set the matchcontains option to true (which was the backend method we used). This ment that when I continued to add more characters it only kept the terms that started with the input query (using the cache - no new ajax request was made). However, as I had set the cacheLength to 1 I believe it shouldn't have used the cache at all.
cl2csand
A: 

Hi guys - I had exactly the same problem - cacheLength didn't make any difference no matter what I set it to, it would always be the default of 10. I repeatedly tried it on 1 because I didn't want any caching, but still came back with 10. I randomly tried setting it to 0 and it worked. crazy but true ;o)

Cas
A: 

Have obviously come to this 18 months on, but

cacheLength: 0

in the options worked for me. So maybe latest release has fixed the bug?

LiverpoolsNumber9