首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么bloodhound.get()返回未定义的?

为什么bloodhound.get()返回未定义的?
EN

Stack Overflow用户
提问于 2014-02-05 23:42:59
回答 1查看 11.3K关注 0票数 3

我正在尝试使用typeahead.js v0.10下面的代码

代码语言:javascript
复制
// instantiate the bloodhound suggestion engine
var numbers = new Bloodhound({
datumTokenizer: function(d) { return d; },
queryTokenizer: Bloodhound.tokenizers.whitespace,
local:  ["(A)labama","Alaska","Arizona","Arkansas"]
});

// initialize the bloodhound suggestion engine
numbers.initialize();
console.log(numbers.get('a'));

实际上,我试图解决这个问题:https://github.com/bassjobsen/Bootstrap-3-Typeahead/issues/26 --我期望如下所示的东西--应该是可能的:

代码语言:javascript
复制
 $('.typeahead').typeahead(
{
items: 4,
source:function(query){return numbers.get(query)}       
});

更新

示例。使用ttAdapter()设置“提前输入”的源。此函数还可用于为source设置引导-3-提前输入属性(该属性接受字符串数组或函数)。

代码语言:javascript
复制
// instantiate the bloodhound suggestion engine
var numbers = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,//function(d) { return d; },
queryTokenizer: Bloodhound.tokenizers.whitespace,
local:  ["(A)labama","Alaska","Arizona","Arkansas","Arkansas2","Barkansas"]
});

// initialize the bloodhound suggestion engine
numbers.initialize();

$('.typeahead').typeahead(
{
items: 4,
source:numbers.ttAdapter()  
});

bloodhound.js显示:

代码语言:javascript
复制
  ttAdapter: function ttAdapter() {
                return _.bind(this.get, this);
            }

因此,ttAdapter()返回一个函数(get()),该函数可以由具有查询作为参数的源设置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-06 00:57:31

我按如下方式实现了Bloodhound.get() (还请参阅下面的命令:http://jsfiddle.net/Fresh/HS9Wy/):

代码语言:javascript
复制
// instantiate the bloodhound suggestion engine
var numbers = new Bloodhound({
    datumTokenizer: function (d) {
        return d;
    },
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    local: ["(A)labama", "Alaska", "Arizona", "Arkansas"]
});

// initialize the bloodhound suggestion engine
numbers.initialize();

// Get an array of datums which satisfy the query for 'a'
numbers.get('a', function (suggestions) {
    jQuery.each(suggestions, function (index, item) {
        console.log(item);
    });
});

调用"get()“的问题是

代码语言:javascript
复制
numbers.get('a')

当你让血犬执行“a”的查询时,你不会对结果做任何事情。要指示"get()“做一些有用的事情,您需要将结果发送到输出函数。见这里的文件

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21591086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档