Skip to main content

Search and Top Navigation

#5398 closed bug (fixed)

Opened March 22, 2010 09:54AM UTC

Closed April 23, 2010 04:20PM UTC

Last modified May 20, 2010 11:15AM UTC

Remote-with-cache demo does not break if (cache.term == request.term) but executes another request

Reported by: thewolfram Owned by:
Priority: minor Milestone: 1.8.2
Component: ui.autocomplete Version: 1.8
Keywords: demo Cc:
Blocked by: Blocking:
Description

Small error in a demo:

In case the new search term and the cached term are equal or the new term contains the cached term, the cached data is (partially) used but another GET is executed nevertheless. The $.ajax should go in an else block.

MacOSX, FF 3.6

Attachments (0)
Change History (4)

Changed April 15, 2010 02:16AM UTC by lambacck comment:1

The if condition is also probably needs tweaking. It currently says:

if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
    response($.grep(cache.content, function(value) {
        return matcher.test(value.value)
    }));
}

It should be:

if (new RegExp($.ui.autocomplete.escapeRegex(cache.term)).test(request.term) && cache.content && cache.content.length < 13) {
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
    response($.grep(cache.content, function(value) {
        return matcher.test(value.value);
    }));
    return;
}

Note that cache.term in the if condition is now wrapped in a call to $.ui.autocomplete.escapeRegex to match the matcher RegExp?. Also the original bug is fixed by adding a return to the if block.

Also matcher tests value.value, this is a deviation from how the list of object works in the autocomplete itself. It uses:

return matcher.test( value.label || value.value || value );

Changed April 15, 2010 02:33AM UTC by lambacck comment:2

Oh yeah. The first if block can also get a return statement after the response call. We could also improve the demo by creating a make cached autocomplete function. Something like:

function caching_auto_complete(item, url, options) {
    var cache = {};
    options.source = function(request, response) {
        if (cache.term == request.term && cache.content) {
            response(cache.content);
            return;
        }
        if (new RegExp($.ui.autocomplete.escapeRegex(cache.term)).test(request.term) && 
                cache.content && cache.content.length < 13) {
            var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
            response($.grep(cache.content, function(value) {
                // NOTE change to match core autocomplete logic
                return matcher.test(value.label || value.value || value)
            }));
            return;
        }
        $.ajax({
            url: url,
            dataType: "json",
            data: request,
            success: function(data) {
                cache.term = request.term;
                cache.content = data;
                response(data);
            }
        });
    }
    $(item).autocomplete(options);
    
}

caching_auto_complete('#birds', "search.php", {
            minLength: 2,
            select: function(event, ui) {
                log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : 
                    "Nothing selected, input was " + this.value);
            }});

This is almost completely untested.

Changed April 23, 2010 04:20PM UTC by scottgonzalez comment:3

milestone: TBD1.9
resolution: → fixed
status: newclosed

Fixed in e087e7d.

Changed May 20, 2010 11:15AM UTC by rdworth comment:4

milestone: 1.91.8.2