Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typeahead 0.10 prevent caching

I use twitter's typeahead 0.10 with remote url to retrieve JSON results from server.

I would like to prevent tthe client caching so that the search takes place always on the server. How can I do that?

Please see below my code:

 // instantiate the bloodhound suggestion engine
    var dataSource = new Bloodhound({
        datumTokenizer: function (d) {
            return Bloodhound.tokenizers.whitespace(d.value);
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url: "../" + autocompleteInfo.ControllerName + "/" + autocompleteInfo.MethodName + "?term=%QUERY&ts=" + (new Date().getTime()),
            filter: function (res) {
                var data = [];
                data = $.map(res, function (item) {
                    return { label: item.Name, id: item.Id, autocompleteInfo: autocompleteInfo, cssClass: item.Class };
                });

                return data;
            }
        },
        limit: 15,
        name: 'typeaheadSourceCache',
        ttl: 0,
        ajax: {
            cache: false
        }
    });

    dataSource.initialize();

    $("#" + autocompleteInfo.AutocompleteId).typeahead({
        minLength: 3,
        highlight: true,
        autoselect: true
    },
        { 
            displayKey: 'label',
            source: dataSource.ttAdapter(),
            templates: {
                suggestion: Handlebars.compile(
                '<div class="searchItem {{cssClass}}">{{label}}</div>'
                )
            }
        });
like image 857
Vicentiu Berneanu Avatar asked Feb 18 '14 14:02

Vicentiu Berneanu


People also ask

What is a Typeahead input?

The typeahead input fields are very popular in modern web forms. The main purpose of using typeahead is to improve the user experience by supplying hints or a list of possible choices based on the text they've entered while filling a form or searching something — like the Google instant search.

What is bloodhound Typeahead?

Bloodhound is the typeahead. js suggestion engine. Bloodhound is robust, flexible, and offers advanced functionalities such as prefetching, intelligent caching, fast lookups, and backfilling with remote data.

What is Typeahead API?

Performs search to retrieve list of places by input text and location vicinity. Address Autocomplete takes free form text as input. It could be a part of an address. Location could be provided either as latitude/longitude or as country ISO code. It returns consolidated list of addresses based on the input text.


2 Answers

Just add cache field to remote object:

remote: { 'cache': false ... }

like image 53
Richard Bdžoch Avatar answered Sep 27 '22 16:09

Richard Bdžoch


Look at version 10.0.2. There is now a means to clear cache via Bloodhound.js (used in association with Typeahead.js):

engine.clearRemoteCache();

Here is the documentation from twitter typeahead: https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#bloodhoundclearremotecache

like image 41
Phil Avatar answered Sep 27 '22 16:09

Phil