Skip to content

Error calculating rendered variable in async function. Succesive calls to this funcion not appending the suggestions: #249

@xelloss1012

Description

@xelloss1012

Hello!,

I noticed an error in the async function when calculating the value for the rendered variable... This makes succesive calls to this funcion not appending the suggestions:

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

This way, the result of rendered - limit it's always added to rendered, so rendered will always end up having the value of limit!... For example, let's say we have already rendered 10 suggestions in a previous call to async, and that the limit is set to 1000. Then idx will be calculated as 1000-10=990... and thus when adding this amount to rendered (whose value is 10) the result will be again 1000 (the limit). This would make the next call to async to be over the limit and the new suggestions will not be added.

In my Iocal file I have solved it this way:

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

It is as simple as applying a Math.max between the result of rendered - limit and suggestion.length.

Hope this helps someone.

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions