typeahead.js icon indicating copy to clipboard operation
typeahead.js copied to clipboard

how to prevent AJAX requests when user selects an item

Open chrisdlangton opened this issue 12 years ago • 7 comments

When the selections are displayed via local or prefetch or even remote, as long as there is a remote setup there is always an additional AJAX request sent when the user clicks on the value they want or tabs to use the auto-complete value.

Is there a way to prevent that final AJAX call? the AJAX is looking up matching values, where the term it sends is a term it knows matched a request an instant ago.. OR it is a value offered from a json prefetch source and may not even match a value from the remote source so the AJAX is error prone.

To remove this additional redundant AJAX request my boss and I spent hours...

We tried the Custom Events typeahead:autocompleted and typeahead:selected to return false or block ajax to no avail. We would have liked to use the event object and preventDefault() but that is not an option... We tried the native events also to no avail.

Really annoying that these redundant network requests are made, specificity for selected values that were of a local or prefetched sources...

chrisdlangton avatar Dec 11 '13 01:12 chrisdlangton

Its been a 5 days, can I please get a response from the maintainers?

chrisdlangton avatar Dec 15 '13 22:12 chrisdlangton

I'm having the same issue. Any help would be appreciated.

tetronomis avatar Dec 25 '13 23:12 tetronomis

One thought here: Let's say you are working on list of people’s names. One name in the list is Al and another one Albert. If in your case you type in Al or for that matter select Al from your list then Al becomes the next query. So in this case the next time you open the input box you would like to see both Al and Albert in the suggestion list, right ? In this case we simply cannot put 'Al' in the cache list for the query 'Al' without destroying Albert’s chance of ever getting picked. So if you want the suggestion lookup to go quiet once input is selected then you would have to implement some sort of index on what suggestions have ben executed and refer to that one on re-focus on the field. This requires a re-design on the suggestions calculations since they are currently calculated each time the input value is changed.

Second part that also may complicate things is a bug that occurs when you select a dropdown item with a mouse click. In that case the input field loses focus and is then refocused. So your options here is to require a re-design on the suggestions calculations or just to tolerate the extra hit on your remote server (it should run in the background without disturbing the browser user).

There are a lot of quite urgent issues with this control that I would prioritize over this one.

However... this could be an idea for implementation on the next version of the control: Index in the InputView object on what queries have been executed. On focus of the input box the getSuggestions function would look up if the query has been executed or not. If it has been executed results from the cache are fetched from the cache, if not the remote search is executed. The handleSelection function would then not trigger the execution on getSuggestions.
This implementation would also have the benefit of not requiring implementers to wait for the initialized event on the input box to be executed before initial value is set to a input box (via the typeahead's setQuery method). Using JQuerie's val('init-value') would also be an option without sideaffects as it is today.

Svakinn avatar Dec 26 '13 00:12 Svakinn

Duplicate of #188. This has been fixed in intergration-0.10.0.

jharding avatar Dec 27 '13 22:12 jharding

Unfortunately that issues makes no mention of the problem where a prefetch source term is the selected term, and incorrectly attempts to fetch that term from the remote if a remote exists.

chrisdlangton avatar Dec 27 '13 22:12 chrisdlangton

Hmm, maybe I'm misunderstanding something. It seems to me that the root issue for both this issue and #188 is that an extra ajax request is sent when a suggestion is selected from the dropdown menu. Is that not correct?

If you take a look at http://twitter.github.io/typeahead.js/beta/examples/ and try out the "Prefetch Backed by Remote" example, you'll see that extra ajax requests are no longer happening.

jharding avatar Dec 28 '13 04:12 jharding

Thanks Jake, I didn't see the two as the sane but after reviewing the example I can see and agree the root cause solves both issues. Cheers

chrisdlangton avatar Dec 28 '13 05:12 chrisdlangton