Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

@skwp
Copy link

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!

@perryzheng
Copy link

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
Copy link

plentz commented Oct 27, 2012

+1

jhanggi added a commit to jhanggi/ajax-chosen that referenced this issue Jan 14, 2013
@jhanggi jhanggi mentioned this issue Jan 14, 2013
jlavallee pushed a commit to jlavallee/ajax-chosen that referenced this issue Feb 7, 2013
jlavallee pushed a commit to jlavallee/ajax-chosen that referenced this issue Feb 7, 2013
@fredwu fredwu mentioned this issue Feb 8, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants