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

problem with source async

Open ghost opened this issue 11 years ago • 16 comments

$('#input-rms').typeahead({
  hint: true,
  highlight: true,
  minLength: 2,
  classNames: {
    menu: 'tt-dropdown-menu'
  }
},
{
  name: 'rms',
  limit: 10,
  source: function (q, sync, async) {
    $.ajax('/api/getrms?name='+q, // it will return a list of data of length 7
           {
            success: function(data, status){  async(data); }
           }
          );
  }
});

Problem: The ajax url will return a list of data of length 7, however, the suggestions only render the first 3 results.

Can anyone gives a example about how to use the async in source.

ghost avatar Apr 26 '15 14:04 ghost

Hmm, I'm not sure what's causing your problem. Here's an example I just whipped up that should be functionally equivalent to your code. It works as expected.

Can you test your code in an incognito window? Just to make sure there's nothing goofy going on with caching. Also, if you could point me towards a page that demonstrates your problem, I can help debug.

jharding avatar Apr 26 '15 21:04 jharding

I tested in incognito window it doesn't work.

And I look at the source code of async function source code in typeahead.bundle.js. In line 1705, rendered += suggestions.length is putted before that._append(query, suggestions.slice(0, that.limit - rendered));.

I think it is this line cause the problem. When I putted it after that._append(query, suggestions.slice(0, that.limit - rendered)); It works the way I want.

function async(suggestions) {
                    suggestions = suggestions || [];
                    if (!canceled && rendered < that.limit) {
                        that.cancel = $.noop;
                        rendered += suggestions.length;
                        that._append(query, suggestions.slice(0, that.limit - rendered));
                        that.async && that.trigger("asyncReceived", query);
                    }
                }

ghost avatar Apr 27 '15 10:04 ghost

Yeah... I'm not sure why that line of code exists. I'll get it fixed, thanks for the report and thanks for digging into the problem. Very helpful.

jharding avatar Apr 28 '15 08:04 jharding

Posted a PR with a fix, see #1200. Here's a version of typeahead.bundle.js with the fix in case you want to try it out before it is released.

jharding avatar Apr 28 '15 08:04 jharding

Ok, thanks

ghost avatar Apr 28 '15 09:04 ghost

+1! The patch works like a charm! Can you please release it?

nostalgiaz avatar May 04 '15 16:05 nostalgiaz

Release please!

martino avatar May 06 '15 08:05 martino

Can confirm this is a problem. It's exacerbated when you only use async and your remote returns exactly limit results. In this case rendered - limit = 0, so (0,0) gets passed to suggestions.slice

jormon avatar May 18 '15 23:05 jormon

For what it's worth, changing it this way makes async() structurally similar to sync(), and also keeps rendered updated correctly:

typeahead.bundle.js 1719    function async(suggestions) { 1720        suggestions =(suggestions || []).slice(0, that.limit - rendered); 1721        if (!canceled && suggestions.length) { 1722            that.cancel = $.noop; 1723            rendered += suggestions.length; 1724            that._append(query,suggestions); 1725            that.async && that.trigger("asyncReceived", query); 1726        } 1727    }

jlidbeck avatar May 20 '15 03:05 jlidbeck

Fixed by #1212

jarthod avatar May 26 '15 16:05 jarthod

Is it just me who has a problem with this fix? Whenever rendered is smaller than limit, this is failing. eg. rendered = 3; that.limit = 5

suggestions =(suggestions || []).slice(0, that.limit - rendered) => suggestions.length = 2

I don't know if I'm doing something wrong but for what it's worth, here's the work around I implemented:

function async(suggestions) {
    var howMany;
    suggestions = suggestions || [];
    if (!canceled && rendered < that.limit) {
       that.cancel = $.noop;
       rendered += suggestions.length;
       howMany = (rendered <= that.limit) ? rendered : that.limit - rendered;
       that._append(query, suggestions.slice(0, howMany));
       that.async && that.trigger("asyncReceived", query);
    }
}

sebsonic avatar Jun 16 '15 00:06 sebsonic

@Sebsonic , I believe that's the intended behavior--limit is the total number of suggestions to render from all sources combined.

jlidbeck avatar Jun 17 '15 17:06 jlidbeck

oh my god thank you guys so much for this fix. I was tearing my hairout with this one.

dessalines avatar Jun 26 '15 02:06 dessalines

+1 I also am experiencing this problem, and had tracked down the problematic code. Great to see there's a fix!

Glavin001 avatar Jul 07 '15 17:07 Glavin001

+1 for pushing this fix. I also spent over an hour digging into the source of 0.11.1 and came to the same conclusion that the "rendered +=" line needs to be moved down.

staticglobal avatar Aug 19 '15 23:08 staticglobal

Struggled with this problem for a long time today. Moving the line seems to fix it. How come it isn't being fixed in the release?

skovmand avatar May 12 '16 12:05 skovmand