I know that it works. I have implemented it successfully.
$("#vehicle_application_product_id").autocomplete({
minLength: 2,
source: function(request, response) {
var term = request.term;
if (term in cache) {
response(cache[term]);
return;
}
lastXhr = $.getJSON("/products/get_json_list", request, function(data, status, xhr) {
cache[term] = data;
if (xhr === lastXhr) {
response(data);
}
});
}
});
I am like a script kiddie with some of this stuff. I would NEVER be able to write that anonymous function myself, because of the following reasons:
1) How did this person know to put the parameters "request" and "response" in the spec?
2) The lines beginning "lastXhr = " is really mystifying. There is yet another anonymous function at the end. I realize that is probably a callback, something that is executed after the request is sent to the specified URL.
3) That last conditional in the anonymous function:
if ( xhr === lastXhr ) {
response( data );
}
What the heck is that? If then third parameter, xhr, from this anonymous block is equal to the vaue returned by the .getJSON call ENCOMPASSING this anonymous block, return the data in the response?
That is really the most difficult part to wrap my head around. And I notice that's 3 equals signs. This looks like pretty advanced code concepts, I would just appreciate what the rationale behind this is so that I can become a better coder.