【问题标题】:Why does bloodhound.get() return undefined?为什么 Bloodhound.get() 返回未定义?
【发布时间】:2014-02-05 23:42:59
【问题描述】:

我正在尝试将下面的代码与 typeahead.js v 0.10 一起使用

// 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 我希望如下所示的事情应该是可能的:

 $('.typeahead').typeahead(
{
items: 4,
source:function(query){return numbers.get(query)}       
});

更新

examples。使用ttAdapter() 设置预输入源。 此函数还可用于为Bootstrap-3-Typeahead 设置source 属性(接受字符串数组或函数):

// 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 显示:

  ttAdapter: function ttAdapter() {
                return _.bind(this.get, this);
            }

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

【问题讨论】:

    标签: jquery typeahead typeahead.js bloodhound


    【解决方案1】:

    我实现 Bloodhound.get() 如下(另见此小提琴:http://jsfiddle.net/Fresh/HS9Wy/):

    // 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()”的问题,即

    numbers.get('a')
    

    当你让 Bloodhound 执行“a”的查询时,你没有对结果做任何事情。要指示“get()”做一些有用的事情,您需要将结果发送到输出函数。请参阅documentation here。

    【讨论】:

    • 感谢您的回答。但实际上这仍然没有返回任何东西?我终于从例子中找到了twitter.github.io/typeahead.js/examples。 ttAdapter()返回一个函数,可以用来设置Bootstrap-3-Typeahead的source
    • 正确,“get()”不返回任何内容。你的问题特别问为什么“get()”没有返回任何东西,我解释说你使用不正确。很高兴您在查看基本示例后找到了 ttAdapter。
    猜你喜欢
    • 1970-01-01
    • 2014-10-24
    • 2021-12-11
    • 2019-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2018-07-11
    相关资源
    最近更新 更多