Typeahead 0.10 prevent caching
Asked Answered
P

4

6

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>'
                )
            }
        });
Porringer answered 18/2, 2014 at 14:12 Comment(2)
It may be of interest to know that others in the twitter typeahead community have the same problems and solutions are being examined. For example, see: github.com/twitter/typeahead.js/pull/703Devindevina
see also github.com/twitter/typeahead.js/issues/564Porringer
D
6

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

Devindevina answered 14/3, 2014 at 21:17 Comment(0)
T
29

Just add cache field to remote object:

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

Tyrrell answered 2/1, 2015 at 10:55 Comment(4)
This is exactly what I needed. I had multiple bloodhound engines, tied to multiple text inputs, each serving up a different list of users by role (that's a mouthful). After choosing a user from one of the three text inputs, the other inputs would show the list of users that the first input had served up. This was actually logically incorrect for my purposes. Anyway, thanks Richard.Selfassured
This should be selected as the answer. Thanks Richard.Iconography
This should definitely be the selected answer. Why on earth wasn't this option mentioned under "Remote" in the docs at github.com/twitter/typeahead.js/blob/master/doc/…Ingaborg
This definitely works! Just keep in mind that to refresh the results (in case you added more results to your data source) you have to change at least one letter of the current typeahead search. Thank you very much!Annunciate
D
6

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

Devindevina answered 14/3, 2014 at 21:17 Comment(0)
S
0

Try to use typeahead destroy utils, i think in your case are:

$("#" + autocompleteInfo.AutocompleteId).typeahead('destroy');

The you reinizialize $("#" + autocompleteInfo.AutocompleteId)

Storied answered 18/2, 2014 at 15:18 Comment(1)
Thank you. I do not want to destroy and recreate again the typeahead control(or at least the input element used by typeahead), one reason for this being that I don't have a proper event when to do this destroy/create operation. I want for each keyword that is searched, event if it was searched before, to make a request to the server.Porringer
T
0

To fix IE issues I've came to:

remote: {
url: '/myurl?par=%QUERY',
wildcard: '%QUERY',
prepare: function (q, o) {
        o.url = o.url.replace('%QUERY', encodeURIComponent(q));
        o.cache = false;
        return o;
    }
}

prefetch: {
    url: '/myurl2',
    ttl: 300000, //5min
    thumbprint: userName,
    prepare: function(o) {
        o.cache = false;
        return o;
}
Torquay answered 15/8, 2017 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.