Use Meteor collections for Typeahead Bloodhound, preferably without making my own API

落花浮王杯 提交于 2019-12-04 12:27:26
Niall Farrington

This code posting uses:

    local: [{ val: 'dog' }, { val: 'pig' }, { val: 'moose' }],
Jeppe Stougaard

Spend quite some time trying to get the tokenfield to work reactively with my Meteor collection, so I'll just post my solution here as well.

I ended up not using Bloodhound at all, but instead just using Meteor directly. I realize that the RegEx search is pretty primitive, but if what you're searching is a collection of tags, it does the job.

var currentTags = []; // Handle this however you wish

$('#tokenfield').tokenfield({
    typeahead: [null, {
        name: 'tags',
        displayKey: 'value',
        source: function(query, syncResults, asyncResults) {

            var suggestedTags = Tags.find({value: {
                $regex: "^"+query,
                $options: "i",
                $nin: currentTags
            }}).fetch();

            syncResults(suggestedTags);
            //Optionally do some server side lookup using asyncResults
        }
    }]
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!