如何在 typeahead.js 中消除 ajax 请求

问题描述

我正在使用 twitter typeahead 库来实现搜索功能

我在 typeahead 中找到了通过 ajax 发送 POST 请求的方法

问题是: 无论多快或多慢,也不管退格键,它都会对我输入的每个单词发出请求。

这是我的代码片段:

$('.typeahead').typeahead({
    hint: true,highlight: true,minLength: 2,},{
    source: function (query,process) {
        return $.ajax({
                url: "Somedomain" + "post/api/skills",type: 'post',data: { query: query,limit: 15 },dataType: 'json',success: function (result) {
                    console.log(result);
                    var resultList = result.skills.map(function (item) {
                        var aItem = { value: item };
                        return aItem;
                    });

                    process(resultList);
                    return;
                }
            });
    },displayKey: 'value',});

我试过了:

像这样在源代码中使用 lodash 库的 debounce,但它不发送任何 ajax 请求。

代码片段:

function debounceIt(query,process) {
    return $.ajax({
                url: "Somedomain" + "post/api/skills",success: function (result) {
                    console.log(result);
                    var resultList = result.skills.map(function (item) {
                        var aItem = { value: item };
                        return aItem;
                    });

                    process(resultList);
                    return;
                }
            });
}

$('.typeahead').typeahead({
    hint: true,minLength: 1,process) {
        _.debounce(function () {
             debounceIt(query,process);
        },300);
    },});

谁能帮忙解决这个问题? 我试图在堆栈溢出上看到类似的帖子,但找不到任何解决方案。

解决方法

我通过使用 Bloodhound 设法解决了这个问题。

结果数据格式为:

Sub aHeadlines()
On Error Resume Next
'Word objects

Dim p As Object
Dim s As String

Dim xl
Dim wb,ws,xlr
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Add
Set ws = wb.Worksheets(1)
i = 1

For Each p In ActiveDocument.Paragraphs
If Len(p) > 200 Then
Set xlr = ws.Range("a" & i)

  p.Range.Sentences(1).copy
  
  xlr.PasteSpecial 3
  
 i = i + 1

End If
Next

End Sub

代码片段:


import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;

public class JavaTextField {

    private JFrame frame;
    private JTextField text1;
    private JTextField text2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaTextField window = new JavaTextField();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JavaTextField() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100,100,450,300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);
        
        text1 = new JTextField();
        text1.setBounds(114,38,122,40);
        frame.getContentPane().add(text1);
        text1.setColumns(10);
        String majorText = text1.getText();
        
        
        text2 = new JTextField();
        text2.setBounds(114,117,86,20);
        frame.getContentPane().add(text2);
        text2.setColumns(10);
        String minorText = text2.getText();
        
        JButton btnButton = new JButton("Button");
        btnButton.setBounds(132,192,159,40);
        frame.getContentPane().add(btnButton);
        btnButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(majorText);
                System.out.println(minorText);
            }
            }
    );
}
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...