JQuery UI Autocomplete Syntax

Posted by djs22 on Stack Overflow See other posts from Stack Overflow or by djs22
Published on 2010-12-26T02:21:50Z Indexed on 2010/12/26 2:53 UTC
Read the original article Hit count: 770

Can someone help me understand the following code? I found it here.

It takes advantage of the JQuery UI Autocomplete with a remote source. I've commented the code as best I can and a more precise question follows it.

    $( "#city" ).autocomplete({
        source: function( request, response )  {
//request is an objet which contains the user input so far
// response is a callback expecting an argument with the values to autocomplete with
            $.ajax({
                url: "http://ws.geonames.org/searchJSON", //where is script located 
                dataType: "jsonp", //type of data we send the script
                data: { //what data do we send the script 
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    name_startsWith: request.term
                },
                success: function( data ) { //CONFUSED!
                    response( 
                        $.map( 
                        data.geonames, function( item ) { 
                                            return {
                                                    label: item.name+(item.adminName1 ? ","+item.adminName1:"")+","+item.countryName,   
        value: item.name
                                            }
                                        }
                        )
                    );
                  }
            });
        }
    });

As you can see, I don't understand the use of the success function and the response callback.

I know the success function literal is an AJAX option which is called when the AJAX query returns. In this case, it seems to encapsulate a call to the response callback? Which is defined where? I thought by definition of a callback, it should be called on its own?

Thanks!

© Stack Overflow or respective owner

Related posts about jquery-ui

Related posts about jquery-ajax