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

component is requesting search results upon intialization #47

Open
matthias-k opened this issue Apr 26, 2017 · 2 comments
Open

component is requesting search results upon intialization #47

matthias-k opened this issue Apr 26, 2017 · 2 comments

Comments

@matthias-k
Copy link

Hi,

I have an app which uses many instances of ember-aupac-typeahead in one page. When I enter the corresponding route, all typeahead components are populated via the selection property. I just noticed that in this moment, all of them trigger the source function to request search results. This doesn't seem to make sense to me, since at this point the user has not interacted with the component and in my case this creates around 50 ajax-requests to the API which take a lot of time to process. Is this on purpose or is it a bug?

I created an ember twiddle to demonstrate the problem:
https://ember-twiddle.com/328c04b3f57f7ba90f7f502447a58927?openFiles=controllers.application.js%2C

The application template shows an ember-aupac-typeahead component which is prefilled via selection. The template shows whether a search request has been triggered so far. It switches to "results have been requested" immediately when the page loads.

Best,
Matthias

@jackmatt2
Copy link
Member

I am guessing that this is happening when the value of the typehead is set.

  setValue : function(selection) {
    if (this.get('_typeahead')) { // Was failing in tests with this probably due to a stray observer
      selection = this.get('transformSelection')(selection);
      if(selection) {
        this.get('_typeahead').typeahead('val', selection); <----------------------HERE
      } else {
        this.get('_typeahead').typeahead('val', '');
      }
    }
  },

Not sure how to fix this thought because there is nothing in the typeahead documention about this. I found an issue here with a similar (not the same) problem.

@matthias-k
Copy link
Author

I think I figured out a way to work around this problem: in setValue, I store the value of selection and the fact that it has not yet been searched for. Then, in source I can check whether query matches the selection and whether the component has already searched for this value. If not, I can ignore the search request. You can check my proof of concept in this ember twiddle.

It is probably not too elegant, but it solves the problem for now. The better solution would likely be to discuss this with the upstream developers of typeahead.js.

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

No branches or pull requests

2 participants