什么是“响应"?和“请求" jQuery UI Autocomplete的"source"参数中打回来? [英] What are the "response" and "request" arguments in jQuery UI Autocomplete's "source" callback?

查看:183
本文介绍了什么是“响应"?和“请求" jQuery UI Autocomplete的"source"参数中打回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看自动完成教程,我有几个问题: http://jqueryui.com/demos/autocomplete/#option-disabled

I'm looking at the autocomplete tutorial, and I have a few questions: http://jqueryui.com/demos/autocomplete/#option-disabled

$( "#tags" )
            // don't navigate away from the field on tab when selecting an item
            .bind( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                        $( this ).data( "autocomplete" ).menu.active ) {
                    event.preventDefault();
                }
            })
            .autocomplete({
                minLength: 0,
                source: function( request, response ) {
                    // delegate back to autocomplete, but extract the last term
                    response( $.ui.autocomplete.filter(
                        availableTags, extractLast( request.term ) ) );
                },
                focus: function() {
                    // prevent value inserted on focus
                    return false;
                },
                select: function( event, ui ) {
                    var terms = split( this.value );
                    // remove the current input
                    terms.pop();
                    // add the selected item
                    terms.push( ui.item.value );
                    // add placeholder to get the comma-and-space at the end
                    terms.push( "" );
                    this.value = terms.join( ", " );
                    return false;
                }
            });

所以我知道source的参数是requestresponse.这些是保留关键字吗?在Google上输入此内容时,我找不到任何内容.我不清楚传递到这里的请求和响应是什么.请求只是获取输入内容吗?我在哪里可以读到更多?

So I understand the parameters for source is request and response. Are these reserved keywords? I couldn't find anything when typing this into google. I am unclear as to what is the request and response being passed into here. Is the request just grabbing the input? Where can I read up more on this?

推荐答案

requestresponse只是代码作者选择给分配给<的回调的两个形式参数的名称.自动完成小部件的href ="http://jqueryui.com/demos/autocomplete/#option-source"> source 选项:

request and response are simply the names that the author of the code has chosen to give to the two formal parameters of the callback assigned to the source option of the autocomplete widget:

只需指定source选项,即可自定义

自动完成功能以处理各种数据源.数据源可以是:

Autocomplete can be customized to work with various data sources, by just specifying the source option. A data source can be:

  • 具有本地数据的数组
  • 一个字符串,指定一个URL
  • 回叫
  • an Array with local data
  • a String, specifying a URL
  • a Callback

第三个变体,即回调,提供了最大的灵活性,并且 可用于将任何数据源连接到自动完成".回调 得到两个参数:

The third variation, the callback, provides the most flexibility, and can be used to connect any data source to Autocomplete. The callback gets two arguments:

  • 具有单个属性"term"的请求对象,该对象引用 到当前文本输入中的值.例如,当用户 在城市字段中输入"new yo",自动填充"字词将等于 "New yo".
  • 响应回调,它需要一个参数 包含向用户建议的数据.此数据应过滤 基于所提供的术语,并且可以采用所描述的任何格式 上面的简单本地数据(字符串数组或对象数组与 标签/值/两个属性).提供自定义时很重要 源回调以处理请求期间的错误.你一定要 即使遇到错误也要调用响应回调.这 确保小部件始终具有正确的状态.
  • A request object, with a single property called "term", which refers to the value currently in the text input. For example, when the user entered "new yo" in a city field, the Autocomplete term will equal "new yo".
  • 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.

这篇关于什么是“响应"?和“请求" jQuery UI Autocomplete的"source"参数中打回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆