问题描述
我想构建一个类似于StackOverflow的标签输入。我正在尝试使用Meteor集合作为 ,因为我最终希望使用。
I want to build a tags input like the one in StackOverflow. I am trying to use Meteor collections as the remote or prefetch data for Typeahead Bloodhound because I want to eventually use Bootstrap Tokenfield.
根据他们的文档和示例,绝对需要JSON数据的网址。我怎样才能(最好是反应性地)将数据提供给Bloodhound?我已经研究了,但我不知道如何将其与。
According to their documentation and examples, a url to the JSON data is absolutely required. How can I provide the data, preferably reactively, to Bloodhound? I have looked into the Meteor Typeahead package, but I can't figure out how to use it with the Meteor Tokenfield package.
以下是我尝试执行的操作,但是没有用。 :(
Below is what I've tried to do, but it doesn't work. :(
<div class="control-group">
<label class="control-label" for="users">Users</label>
<div class="controls">
<input type="text" class="form-control" id="tokenfield-typeahead-users" value="" />
</div>
</div>
Template.viewUsers.rendered = function() {
var users = new Bloodhound({
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.username);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 20,
remote: {
// url points to a json file that contains an array of tokens
url: function() {
return Meteor.users.find().fetch().map(function(user){ return user.username; });
}
}
});
users.initialize();// kicks off the loading/processing of `local` and `prefetch`
// passing in `null` for the `options` arguments will result in the default
// options being used
$('#tokenfield-typeahead-users').tokenfield({
typeahead: [null, {
name: 'users',
displayKey: 'username',
source: users.ttAdapter()
// `ttAdapter` wraps the suggestion engine in an adapter that
// is compatible with the typeahead jQuery plugin
}]
});
};
我宁愿不构建API,但是如果需要的话,如何提供数据?
I prefer not to build an API, but if I have to, how do I provide the data?
推荐答案
使用:
local: [{ val: 'dog' }, { val: 'pig' }, { val: 'moose' }],
这篇关于将流星集合用于Typeahead猎犬,最好不要制作自己的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!