JQuery UI 自动完成语法

发布于 2024-10-09 01:11:32 字数 1566 浏览 0 评论 0原文

有人可以帮我理解下面的代码吗?我在这里找到了它。

它利用远程源的 JQuery UI 自动完成功能。我已尽我所能对代码进行了评论,后面还有一个更精确的问题。

    $( "#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
                                            }
                                        }
                        )
                    );
                  }
            });
        }
    });

正如你所看到的,我不明白成功函数和响应回调的使用。

我知道 success 函数文字是一个 AJAX 选项,当 AJAX 查询返回时会调用它。这样的话,好像封装了一个响应回调的调用?哪个是在哪里定义的?我认为根据回调的定义,它应该被单独调用?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-10-16 01:11:32

文档(“概述”页面)定义的 response 对象:

响应回调,期望
包含数据的单个参数
向用户建议。这个数据应该
根据提供的信息进行过滤
术语,并且可以采用任何格式
上面描述了简单的本地数据
(字符串数组或对象数组
标签/值/两个属性)。它是
提供定制时很重要
处理错误的源回调
在请求期间。你必须始终
即使您调用响应回调
遇到错误。这确保了
小部件始终具有正确的
状态。

因此,“response”参数实际上是一个回调,必须在 ajax 检索自动完成项成功时调用它。

由于您的数据将通过 AJAX 返回,因此您的代码必须手动更新小部件。 jQueryUI 提供一个参数作为函数,以便您的代码可以通过调用该函数来进行更新。

您可以看到用于 source 选项的函数声明中定义的 response 对象:

source: function( request, response )

您甚至可以将 AJAX 调用从等式中剔除,并执行如下操作:

source: function(request, response) {
    response([{label:'foo', value: 'foo'},{label:'bar', value:'bar'}]);
}

将立即使用小部件的标签/值对数组调用 response 回调。

The response object as defined by the documentation ("Overview" page):

A response callback, which expects a
single argument to contain the data to
suggest to the user. This data should
be filtered based on the provided
term, and can be in any of the formats
described above for simple local data
(String-Array or Object-Array with
label/value/both properties). It's
important when providing a custom
source callback to handle errors
during the request. You must always
call the response callback even if you
encounter an error. This ensures that
the widget always has the correct
state.

so, the 'response' argument is actually a callback, which must be called upon success of the ajax retrieval of autocomplete items.

Since your data will come back via AJAX, your code must update the widget manually. jQueryUI provides an argument as a function so that your code can do that update by calling the function.

You can see the response object defined in the declaration of the function used for the source option:

source: function( request, response )

You could even take the AJAX call out of the equation and do something like this:

source: function(request, response) {
    response([{label:'foo', value: 'foo'},{label:'bar', value:'bar'}]);
}

Would immediately call the response callback with an array of label/value pairs for the widget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文