简体   繁体   English

使用Bloodhound-this.source的Typeahead js不是一个函数

[英]Typeahead js with Bloodhound- this.source is not a function

I'm following this example for typeahead.js using Bloodhound to the T, but I'm getting a javascript error. 我正在使用Bloodhound到T的typeahead.js 这个例子 ,但是我收到了一个javascript错误。 What am I missing? 我错过了什么?

HTML: (.net razor view) HTML :( .net razor view)

@Scripts.Render(Links.Scripts.typeahead_bundle_js) 
@Styles.Render(Links.Content.typeahead_min_css)
<input id="myInput" type="text" class="form-control" />

JS: JS:

$(function () {
    var data = ["abce", "abcd", 'def', 'abcdef'];
    var bh = new Bloodhound({
        local: data,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        datumTokenizer: Bloodhound.tokenizers.whitespace
    });
    //bh.initialize(); //this wasn't in the example, adding it had no effect

    $('#myInput').typeahead({
        highlight:true
    },
    {
        name: "testData",
        source: bh
    });
});

gives the error in typeahead.bundle.js : 给出了typeahead.bundle.js的错误:

this.source is not a function this.source不是一个函数

This one gave me a hard time too, because, same as you, I was doing everything to the letter as in the example... I took me a while to check the exact version of the lib that I use and compare it to the one in the example. 这个给了我一个艰难的时间,因为和你一样,我正在做的所有内容都在示例中......我花了一些时间检查我使用的lib的确切版本并将其与示例中的一个。 I was using the typeahead.js 0.10.5 packaged in the 'twitter-typeahead-rails' gem, and in the examples the version used is typeahead.js 0.11.1. 我使用打包在'twitter-typeahead-rails'宝石中的typeahead.js 0.10.5,在示例中使用的版本是typeahead.js 0.11.1。

As soon as I switched the version, everything started working as it should. 一旦我切换版本,一切都开始正常工作。 There was even no need to re-map the array of strings into array of objects or to call ttAdapter on the source. 甚至没有必要将字符串数组重新映射到对象数组或在源上调用ttAdapter。 Your code will probably work the way you posted it too... 您的代码可能会以您发布的方式工作...


Quote from twitter-typeahead changelog for the version 0.11..0: 引用来自twitter-typeahead changelog的版本0.11..0:

...There are bunch of API changes with this release so don't expect backwards compatibility with previous versions. ...此版本中有大量API更改,因此不要指望与以前版本向后兼容。 There are also many new undocumented features that have been introduced. 还引入了许多新的未记录的功能。 Documentation for those features will be added before v1 ships. 这些功能的文档将在v1发布之前添加。 ... ...

你必须使用source: bh.ttAdapter()而不是source: bh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM