代码之家  ›  专栏  ›  技术社区  ›  Kenneth D.

twitter typeahead.js process()用未定义的值填充列表

  •  2
  • Kenneth D.  · 技术社区  · 12 年前

    我似乎也有同样的问题 Twitter Typeahead Ajax results undefined 但这还没有解决,所以我再次询问,希望我能提供一些缺失的细节。

    我使用的是独立的typeahead 0.10.2和引导2.3.1。底部为1.6.0。包含的库的完整列表如下:

    <script src="/static/portal/js/jquery-1.9.1.min.js"></script>
    <script src="/static/portal/js/jquery-ui-1.10.2.custom.min.js"></script>
    <script src="/static/portal/js/bootstrap.min.js"></script>
    <script src="/static/portal/js/typeahead.bundle.min.js"></script>
    <script src="/static/portal/js/hogan-2.0.0.min.js"></script>
    <script src="/static/portal/js/underscore-min.js"></script>
    

    第一次尝试时,我根据 http://fusiongrokker.com/post/heavily-customizing-a-bootstrap-typeahead ,一切似乎都正常,直到调用process()。它只包含“未定义”的值(尽管元素数量正确),而不是用我的结果填充自动建议下拉列表。

    $("#create_track").click(function() {
        var names_id_map = {};
        $('#ul_results').addClass('hide');
        $('#create_track_form').removeClass('hide');
        // use debounce from underscore.js to throttle requests to server
        var suggestArtists = _.debounce(function(query, process) {
            var artist_names = [];
            $.get(ARTIST_NAMES_URL, { q: query }, function (data) {
                // data = {"results": [{"id": "artist_123", "name": "xyz"}, ...]}
                $.each(data, function (key, val) {
                    if (key == "results") {
                        $.each(val, function (i, item) {
                            artist_display = item.name + ' - ' + item.id;
                            artist_names.push(artist_display);
                            names_id_map[artist_display] = item.id;
                        });
                    }
                });
                process(artist_names);
            });
        }, 300); // rate limit requests to server
    
        $("#artist_name").typeahead(null, {
            name: 'artist-names',
            minLength: 1,
            source: function (query, process) {
                suggestArtists(query, process);
            },
            updater: function (item) {
                console.dir('updater: ' + item); // never output
                $("#artist_id").val(names_id_map[item]);
                // return value which should be selected in drop-down
                // return item;
            },
        });
    
        return false;
    });
    

    该特定教程可能是为typeahead的旧引导捆绑版本创建的。使用typeahead的较新独立版本的文档,我想到了这个,它的行为相同,产生了一个包含正确数量的undefined的菜单:

    $("#create_track").click(function() {
        var names_id_map = {};
        $('#ul_results').addClass('hide');
        $('#create_track_form').removeClass('hide');
    
        var artistNames = new Bloodhound({
            name: 'artist-names',
            datumTokenizer: Bloodhound.tokenizers.whitespace,
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: ARTIST_NAMES_URL + '?q=%QUERY',
                //rateLimitBy: debounce, // debounce is not defined?
                rateLimitWait: 300,
                filter: function(data) {
                    var artist_names = [];
                    // data = {"results": [{"id": "artist_123", "name": "xyz"}, ...]}
                    $.each(data, function (key, val) {
                        if (key == "results") {
                            $.each(val, function (i, item) {
                                artist_display = item.name + ' - ' + item.id;
                                artist_names.push(artist_display);
                                names_id_map[artist_display] = item.id;
                            });
                        }
                    });
                    return artist_names;
                },
            }
        });
        artistNames
            .initialize()
            .done(function() { console.log('artistNames init success'); });
    
        $("#artist_name").typeahead(null, {
            minLength: 1,
            source: artistNames.ttAdapter(),
        });
    
        return false;
    });
    

    如果我用这样的本地数据填充它:

    $("#create_track").click(function() {
        var names_id_map = {};
        $('#ul_results').addClass('hide');
        $('#create_track_form').removeClass('hide');
    
        var artistNames = new Bloodhound({
            name: 'artist-names',
            datumTokenizer: Bloodhound.tokenizers.whitespace,
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            local: ["Abc", "Def", "Ghi", "Jkl"],
        });
        artistNames.initialize()
            .done(function() { console.log('artistNames init success'); });
    
        $("#artist_name").typeahead(null, {
            minLength: 1,
            source: artistNames.ttAdapter(),
        });
    
        return false;
    })
    

    例如,我可以键入“a”或“d”,然后得到一个“未定义”的建议。键入“b”、“c”或“z”不会给出任何建议。

    我已经阅读了所有我能在谷歌上找到的关于提前打字的东西;最后一天也是如此。我相信我的JSON被正确解码,在所有情况下,我的artist_names都是一个字符串的原生JS数组。

    1 回复  |  直到 9 年前
        1
  •  0
  •   Community Mohan Dere    9 年前

    从下面的示例开始 https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md#api (并由其他副本提示 Why does bloodhound.get() return undefined? ),我意识到我的本地值数组实际上需要与datumTokenizer&前面键入的displayKey。我有一个简单的例子,现在正在处理单前导字母建议:

    var artistNames = new Bloodhound({ 
        name: 'artist-names',
        datumTokenizer: function (d) { return d.d; },
        queryTokenizer: Bloodhound.tokenizers.whitespace, 
        local: [{"d": "Abc"}, {"d": "Aaa"}, {"d": "Aab"}, {"d": "Def"}, {"d": "Ghi"}, {"d": "Jkl"}, {"d": "John Coltrane"}], 
    }); 
    artistNames.initialize().done(function() { alert('setup ok'); });
    
    $("#artist_name").typeahead(null, { 
        minLength: 1, 
        displayKey: "d",
        source: artistNames.ttAdapter(), 
    });
    

    我的初始语法来自我通过谷歌找到的一些教程,这些教程可能使用了旧的引导版本的库(例如,我链接到本文@top的教程: “然后将一个简单的名称数组(['John Smith','Jane Smith',…])传递给process()回调。” ).