twitter / typeahead.js

typeahead.js is a fast and fully-featured autocomplete library
http://twitter.github.io/typeahead.js/
MIT License
16.52k stars 3.21k forks source link

twitter-typeahead: Bloodhound prefetch missing support for prepare or transform functions #1701

Open useready-andyv opened 6 years ago

useready-andyv commented 6 years ago

Using: typeahead.js 0.11.1 (typeaheadbundle.js)

I am trying to use bloodhound to prefetch multiple result groups and return them based on a result group selection. Without grouping (just supplying URL) the code works perfectly. When I try to add grouping by giving a prefetch with prepare, the prepare function does not get called.

Code that works (Prefetch with URL only) : var suggestionUniverse = new Bloodhound({ initialize: false, datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Value'), queryTokenizer: Bloodhound.tokenizers.whitespace, prefetch:'./suggestlist.json' }); var promise = suggestionUniverse.initialize(true);//initialize and clear any existing cache. promise.fail(function () { console.log('err, something went wrong :('); });

Code that does not work (Prefetch with prepare) :

var suggestionUniverse= new Bloodhound({ initialize: false, datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Value'), queryTokenizer: Bloodhound.tokenizers.whitespace, prefetch:{ url: './suggestlist.json', prepare: function (settings) { console.log("Called prepare"); return settings; //Optional: Add filter for batchid? }, transform: function(response) { console.log("Called transform"); return response;//add batch filters needed to reduce response. } } }); var promise = suggestionUniverse.initialize(true);//initialize and clear any existing cache. promise.fail(function () { console.log('err, something went wrong :('); });

I don't see any messages in the console, either during initialize (Good, no error) or during the suggestion show (Not good, prepare or transform is not called).

useready-andyv commented 6 years ago

On digging into this issue, I see the following code in typeahead bundle.js Function: parsePrefetch ` var defaults; if (!o) { return null; } defaults = { url: null, ttl: 24 60 60 * 1e3, cache: true, cacheKey: null, thumbprint: "", prepare: .identity, transform: .identity, transport: null };

`

I see the methods as identity stubs whereas in parseRemote,

` o.transform = o.filter || o.transform; o.prepare = toRemotePrepare(o);

` I am diving in to fix this, please let me know if someone has already been there so I dont get hit by a landmine several feet deep in code :-D

If anyone is already fixing this, also let me know so I can help test.

quintet commented 6 years ago

Me also stumbled at this issue. But debugging more, I could find that it must be a mistake at my end. The transform actually works well, but after the first request the data will be cached and request won't be sent again. Please clear your localStorage or set cache: false during development.

Please refer https://github.com/twitter/typeahead.js/issues/774#issuecomment-37566935