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

Custom lookup function cannot produce string[] of suggestions #837

Closed
nebaughman opened this issue Feb 17, 2022 · 4 comments
Closed

Custom lookup function cannot produce string[] of suggestions #837

nebaughman opened this issue Feb 17, 2022 · 4 comments

Comments

@nebaughman
Copy link

jquery-autocomplete v1.4.11

The documentation implies that lookup suggestions can be provided as a string[] or an array of { value: string, data: any } objects. When using a serviceUrl, transformResult can produce a response like:

{ 
  suggestions: string[] 
}

However, when using a lookup function, the response (given to the done function) cannot be formatted like that. It must be formatted as:

{ 
  suggestions: [
    { value: string, data: any },
    ...
  ]
}

The data properties are optional, but you cannot just use a string[] for the suggestions.

For instance:

// this one fails
myLookupFn(query, done) { 
  done({ suggestions: [ "Animal", "Apple", "Avalanche" ] })
}

// this one works
myLookupFn(query, done) {
  done({
    suggestions: [
      { value: "Animal" },
      { value: "Apple" },
      { value: "Avalanche" },
    ]
  })
}

Please clarify the documentation or (better) allow the lookup function to produce the simpler { suggestions: string[] } format. Thanks!

@tkirda
Copy link
Member

tkirda commented Feb 17, 2022

There is a sample in a readme that shows this. Because final format is always suggestion object.

You can always open a PR with an updated documentation.

@nebaughman
Copy link
Author

Thank you for your quick reply. I think the issue, then, is that lookup must return results in the "final format" suggestion object, which does not support string[] of suggestions.

Response Format in the documentation mentions that:

Alternatively, if there is no data you can supply just a string array for suggestions

... which is misleading, because it's not true for the return type of lookup. Is this intentional? I wouldn't want to add special-cases to the documentation if this is just an unintentional bug. Thanks for clarifying.

@tkirda
Copy link
Member

tkirda commented Feb 18, 2022

No it is not intentional, this issue never came up. Just how it was done initially.

@nebaughman
Copy link
Author

Ok, thanks for clarifying. Looking at the code, the done callback given to lookup is the getSuggestions() function. Seems that, by this time, the results must be in the final format you mentioned (cannot accept a string[] at this point). I'm not very familiar with the inner workings (yet?), but maybe the workflow could be altered to handle string[] at that point. Also, instead of a done callback, this could be handled with a Promise. I'll look into it when I have some time. Thanks for helping me understand the intent.

@tkirda tkirda closed this as completed Feb 18, 2022
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

2 participants