Typeahead.js substringMatcher 函数说明 [英] Explanation of Typeahead.js substringMatcher function

查看:40
本文介绍了Typeahead.js substringMatcher 函数说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在研究 Typeahead.js,它是一个非常酷的库.由于文档也非常好,我已经设法获得了一个基本示例.

I'm just doing some research into Typeahead.js and it's a really cool library. I've managed to get a basic example working with thanks to the documentation which is also very good.

但是,我想弄清楚以下代码块实际上在做什么?

However I'm trying to get my head around what the following block of code is actually doing?

var substringMatcher = function(strs) {
  return function findMatches(q, cb) {
    var matches, substringRegex;

    // an array that will be populated with substring matches
    matches = [];

    // regex used to determine if a string contains the substring `q`
    substrRegex = new RegExp(q, 'i');

    // iterate through the pool of strings and for any string that
    // contains the substring `q`, add it to the `matches` array
    $.each(strs, function(i, str) {
      if (substrRegex.test(str)) {
        matches.push(str);
      }
    });

    cb(matches);
  };
};

在示例中,它是在将预输入初始化为 source 选项时传入的.我可以理解它正在从文本框中获取输入并将其与数据集进行比较,但我对 qcb 是什么感到有些困惑?

In the example it's passed in when initialising the typeahead as the source option. I can understand that it's taking the input from the textbox and comparing it to the dataset but I'm a little confused as to what q and cb are?

推荐答案

q 是要搜索的值.这被传递到正则表达式匹配器中,并且搜索不区分大小写(i"参数)

q is the value to be searched for. This is passed into the regex matcher, and the search is case-insensitivie (the "i" param)

cb 是回调函数,它返回从搜索中找到的匹配项.

cb is the callback function which returns the matches found from the search.

findMatches 基本上是一个匿名函数,用作 substringMatcher 函数的实现.

findMatches is basically an anonymous function that is used as the implementation of the substringMatcher function.

这篇关于Typeahead.js substringMatcher 函数说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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