The Source callback of the autocomplete function takes an request and a response object as parameters. I couldn’t find any useful information for what these object excatly are and what properties and methods they define.
The Source callback of the autocomplete function takes an request and a response object
Share
I think the best way to understand what is
function(request, response) {...}is to go through the source code of the plugin itself.I’ll try to be as clear as possible, tell me if you need more details or explanations.
1. What is “source”
When you enter some value in the input, the plugin executes a “search” through the private method “_search”
Reading the last line, you can see the plugin expects the “source” property to be a function that it executes passing as
request: an object-literal with one property
termwhich will contain what was entered in the inputresponse: the plugin property
response. This property is a function that by default calls the private method “_response” which is responsible for showing the menu, filtering the list, closing the menu etc.2. Using the “source” option
But reading the documentation, the
sourceoption accepts an array or an url for remotely getting the values… so how does this work ?The plugin initialize
this.sourcethrough the private method_initSource:You can see that in both cases the plugin ends up defining
this.sourceasfunction(request, response) {...}if you provide an array, it executes the
responsemethod to display the menu passing a filtered array usingrequest.term:if you provide an url, it makes the ajax request and upon success, executes the
responsemethod to display the returneddata:otherwise, it uses the provided option value as it is
3. When passing a function(request, response) as “source” option value
So when you call the plugin this way:
You’re actually not providing the plugin with any data !
But you have the opportunity to gather the data the way you want (other that an array or an url), and still have access to the plugin functionnality through the parameters. You have the input content through
request.termand you can execute theresponsecallback to display the results.Example ? The Autocomplete demo pages…
If you go to the Autocomplete Multiple values demo page, the jquery ui team uses this functionnality.
The data is stored in a javascript array
var availableTags = [...];And they define the
sourceoption this way:Filter the
availableTagsarray with a special treatment for handling multiple values in inputcall the
responsefunction to display the filtered array