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

infinite loop when adding a custom jQuery valHook for typeahead #1015

Closed
kneczaj opened this issue Oct 1, 2014 · 1 comment
Closed

infinite loop when adding a custom jQuery valHook for typeahead #1015

kneczaj opened this issue Oct 1, 2014 · 1 comment

Comments

@kneczaj
Copy link

kneczaj commented Oct 1, 2014

Hello!

First I will describe my situation to let you know why this issue is important for me.

I have a form on a web page, to which I import data from external source. Right now I use val() to set value of every field in the form. Unfortuanately there is a bug right now in our code considering setting values with jQuery's val() at fields with typeahead enabled: on blur event the field value is reset to an empty one. It was already described in a couple of issue reports at your project e.g. here: #28

What I want to do to solve the problem is to assign to the valHook of text input type a function which detects if a field is typeahead-enabled and if it is run $field.typeahead('val', value) to properly update the internal query property of typeahead, like this:

var typeaheadValHook = {
    set: function(element, value) {
      var $element = $(element);
      if ($element.data('ttTypeahead') !== undefined) {
        $element.typeahead('val', value);
        return;
      }
      element.value = value;
    },
};

$.valHooks.text = typeaheadValHook;

The main advantage of this solution is transparency. After adding such a hook, I don't need to take care of problems with val() for typeahead-enabled fields, and it will start to work as for other input fields.

Unfortunately, after applying the code above the problem is that setInputValue() which is called at the time of calling $element.typeahead('val', value); calls inside val() from jQuery which makes infinite look.

Is it possible to change setInputValue() from:

setInputValue: function setInputValue(value, silent) {
    this.$input.val(value);
    silent ? this.clearHint() : this._checkInputValue();
}

to:

setInputValue: function setInputValue(value, silent) {
    this.$input[0].value = value;
    silent ? this.clearHint() : this._checkInputValue();
}

This is going to make possible applying the hook above and easily solve problems with value reset on blur event.

Typeahead can be applied only on text fields so the change will not affect the functionality.

@jharding
Copy link
Contributor

I'm going to explore removing the jQuery dependency (see #1180). This would solve this issue. In the meantime, I'd prefer not to make one-off changes like this.

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

No branches or pull requests

2 participants