5

I am trying to implement caching using jQuery UI autocomplete. I am using jQuery 1.4.4 and UI 1.8.6

Here is the basic code that I got working:

$('#searchbox').autocomplete({
    source: function(request, response) {
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

Here is my attempt to get caching to work from looking at the example:

var cache = {},
    lastXhr;
$('#searchbox').autocomplete({
    source: function(request, response) {
        var term = request.term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }));
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = $.map(data, function(item) {
                return {
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    value: item.NAME
                };
            }); 
            if (xhr === lastXhr) {
                response( $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

Any takers out there?

1
  • It is not caching like it is supposed to be. Commented Dec 9, 2010 at 22:21

2 Answers 2

4

Here's my working example of jQuery UI's autocomplete using cache. Hope it helps:

    var cache = {};
    $("#textbox").autocomplete({
      source: function(request, response) {
       if (request.term in cache) {
        response($.map(cache[request.term].d, function(item) {
         return { value: item.value, id: item.id }
        }))
        return;
       }
       $.ajax({
        url: "/Services/AutoCompleteService.asmx/GetEmployees",  /* I use a web service */
        data: "{ 'term': '" + request.term + "' }",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function(data) { return data; },
        success: function(data) {
         cache[request.term] = data;
         response($.map(data.d, function(item) {
          return {
           value: item.value,
           id: item.id
          }
         }))
        },
        error: HandleAjaxError  // custom method
       });
      },
      minLength: 3,
      select: function(event, ui) {
       if (ui.item) {
        formatAutoComplete(ui.item);   // custom method
       }
      }
     });

If you're not doing so by now, get Firebug. It's an invaluable tool for web development. You can set a breakpoint on this JavaScript and see what happens.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Rafael! I see what I was doing was wrong. I can't live without Firebug!
1

The problem lies in my cache[term] when I was trying to throw my $.map function in it because it is not needed.

cache[term] = $.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                });

So here is my final script for those who are still having trouble: I also left all option out of this to avoid any confusion.

var cache = {},
 lastXhr;

$('#searchbox').autocomplete({
    source: function(term, response) {
        var term = term;
        if (term in cache) {
            response($.map(cache[term], function(item) {
                return {
                    /*Format autocomplete to display name and job title, e.g., Smith, John, Web Developer*/
                    label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                    /*Replace the searched value with the value selected.*/
                    value: item.NAME
                };
            }))
            /*I happened to leave this out, which I think what one of the main cause my caching did not work.*/
            return;
        }
        lastXhr = $.getJSON( "getdata.php", request, function(data, status, xhr) {
            cache[term] = data;
            if (xhr === lastXhr) {
                response($.map(data, function(item) {
                    return {
                        label: item.NAME + (item.PRFNM ? ' (' + item.PRFNM + ')' : '') + ', ' + item.JOBTITLE,
                        value: item.NAME
                    };
                }));
            } 
        });
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.