Success callback is rebound on every keyup, causing a recursive call to itself #30

Open
skwp opened this Issue Jun 6, 2012 · 2 comments

Comments

Projects
None yet
3 participants

skwp commented Jun 6, 2012

If you put a console.log inside the success callback (options.success), you'll notice that you're rebinding the callback on every keyup event. Which means on the first autocomplete, your callback is called once, second time twice, then three times, etc.

This is probably not dangerous, but possibly a performance issue and could lead to subtle bugs.

In either case, the success callback should only be bound a single time, when the box is initialized. The only reason you can't do that is because it currently relies on the value of the box in order to put it back after clearing it. The box seems to be cleared by select.trigger("liszt:update") - not sure why that clears the box, or why we can't just leave the search term in the box if nothing is autocompleted.

Not sure what to do about this without understanding why the trigger is clearing out the box, but happy to help if someone points me in the right direction. Thanks!

You can try something like this.

items = callback(data);
var tempValue = field.attr('value');
if (tempValue == val) {
    $.each(items, function (value, text) {
        if ($.inArray(value + "-" + text, selected_values) === -1) {
            return $("<option />").attr('value', value).html(text).appendTo(select);
        }
    });
    select.trigger("liszt:updated");
    field.attr('value', val);
    field.css('width', 'auto');
    return field;
} else {
   console.log("tempValue: " + tempValue + "  val: " + val);
}

Two things I did:

  1. I got rid of the recursive success(data) call.
  2. I only do select.trigger("liszt:updated") if the what the user typed up to that point matches exactly val at the point. Sometimes they could get out of sync. And if you open up your browser, you could see console.log(...) gets called multiple types.

plentz commented Oct 27, 2012

+1

@jhanggi jhanggi added a commit to jhanggi/ajax-chosen that referenced this issue Jan 14, 2013

@jhanggi jhanggi success callback is rebound on every key up.
Should resolve issue #30
549a4fb

jhanggi referenced this issue Jan 14, 2013

Open

Typing fixes #51

@jlavallee jlavallee added a commit to jlavallee/ajax-chosen that referenced this issue Feb 7, 2013

@jhanggi @jlavallee jhanggi + jlavallee success callback is rebound on every key up.
Should resolve issue #30
efa8279

@jlavallee jlavallee added a commit to jlavallee/ajax-chosen that referenced this issue Feb 7, 2013

@jhanggi @jlavallee jhanggi + jlavallee success callback is rebound on every key up.
Should resolve issue #30
731c90f

fredwu referenced this issue Feb 8, 2013

Merged

Tweaks #55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment