Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter jQuery typeahead - How to remove the cache

I made a query like this... and first time, it ran the filter... cool, and it worked...

But now there are more entries, and it seems to be running off of cache. How do I force it to stop using the cache?

var countries = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        url: Url + '/Country/JsonList',
        filter: function (list) {
            return $.map(list, function (country) { return { name: country.Name }; });
        }
    }
});

countries.initialize();

$('.countries.typeahead').typeahead(null, {
    displayKey: 'name',
    source: countries.ttAdapter()
});
like image 399
Jimmyt1988 Avatar asked Feb 24 '14 20:02

Jimmyt1988


2 Answers

I think this is better than the accepted answer:

var countries = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        url: Url + '/Country/JsonList',
        filter: function (list) {
            return $.map(list, function (country) { return { name: country.Name }; });
        },
        cache: false //NEW!
    }
});

countries.initialize();

$('.countries.typeahead').typeahead(null, {
    displayKey: 'name',
    source: countries.ttAdapter() //NOTE: .ttAdapter() is deprecated and will be removed in V1
});
like image 70
Yahya Uddin Avatar answered Nov 14 '22 11:11

Yahya Uddin


Add the ttl property to the filter... and set it to 1 not 0.

var countries = new Bloodhound({
    datumTokenizer: function (d) { return Bloodhound.tokenizers.whitespace(d.name); },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 10,
    prefetch: {
        ttl: 1,
        url: Url + '/Country/JsonList',
        filter: function (list) {
            return $.map(list, function (country) { return { name: country.Name }; });
        }
    }
});

countries.initialize();

$('.countries.typeahead').typeahead(null, {
    displayKey: 'name',
    source: countries.ttAdapter()
});

https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md

like image 8
Jimmyt1988 Avatar answered Nov 14 '22 10:11

Jimmyt1988