Twitter类型的猎犬令牌无法正常工作

时间:2014-04-29 13:54:53

标签: javascript twitter-bootstrap twitter typeahead.js bloodhound

当我输入“东方”,“家庭”字样时,代币无法正常工作。或者'陷阱'它应该建议' Inkoopverenigingen',出了什么问题?

JSON:

[
{
    "value": "Inkoopverenigingen",
    "tokens": [
        "inkoopverenigingen",
        "inwoopverenigingen",
        "east",
        "home",
        "trap"
    ]
},
{
    "value": "Aannemers - Afbraakwerken",
    "tokens": [
        "West",
        "Side",
        "Story"
    ]
},
{
    "value": "Affichageondernemingen",
    "tokens": [
        "West",
        "Side",
        "Story"
    ]
},
{
    "value": "Aanwervingsbureaus",
    "tokens": [
        "West",
        "Side",
        "Story"
    ]
}

的javascript:

    var engine = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    limit: 15,
    prefetch: 'data/typeahead/what.json',
    remote: 'data/typeahead/queries/%QUERY.json'
});

engine.initialize();
engine.clearPrefetchCache();

var template_source = $("#entry-template-what").html();

$('.test .typeahead-what').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },
    {
        name: 'what',
        displayKey: 'value',
        // `ttAdapter` wraps the suggestion engine in an adapter that
        // is compatible with the typeahead jQuery plugin
        source: engine.ttAdapter(),
        templates: {
            empty: no_results,
            suggestion: Handlebars.compile(template_source)
        }
    }
);

HTML:

<div class="test">
                        <input type="text" class="typeahead-what form-control" id="section" name="section">
                    </div>

html模板:

<script id="entry-template-what" type="text/x-handlebars-template">
<div>
    <strong>{{value}}</strong>
    <div class="controls pull-right">
        <span class="glyphicon glyphicon-plus"></span>
        <span class="glyphicon glyphicon-minus"></span>
    </div>
</div>

2 个答案:

答案 0 :(得分:3)

这是解决方案:

        datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.tokens.join(' ')); },

答案 1 :(得分:2)

提一下,engine.clearPrefetchCache();如果JSON文件同时发生了变化,这一点非常重要。