Meteor 0.6.5.1并使用typeahead.js渲染自动完成

时间:2013-09-23 04:43:05

标签: twitter-bootstrap meteor twitter-bootstrap-3 typeahead.js

我正在尝试将typeahead.js与Meteor 0.6.5.1一起使用,但无法让它始终如一地工作。只有在热代码刷新后才会在渲染时填充finalItems(有时只有最好),但是一旦在浏览器中重新加载页面,它就会变空。 finalItems正确加载的时间,typeahead.js正常工作。

HTML

<template name="addProduct">

    <div class="form-group">
        <label for="styleName">Style Name</label>
        <input type="text" id="ta" name="styleName" class="form-control">
    </div>

</template>

的Javascript

Template.addProduct.rendered = function() {
    items = [];
    finalItems = [];

    styles = function() {
        prods = Products.find({}, {
            fields: {
                styleName: 1
            }
        });
        prods.forEach(function(item) {
            items.push(item.styleName);
        });

        finalItems = _.uniq(items);

        console.log(finalItems);

    };

    styles();

    $('#ta').typeahead({
        name: ['styles'],
        local: finalItems
    });

};

2 个答案:

答案 0 :(得分:4)

如果您正在使用常规自由文本自动填充功能,那么您可能需要查看我的自动完成程序包for meteor,几天前首次发布:

  

https://github.com/mizzao/meteor-autocomplete

您可能也会从那里获得一些灵感,因为您正在尝试做什么。我强烈推荐使用Meteor集合支持的实现,而不是尝试使用现有的异步库拼凑一些东西,这就是我在这里所做的。

使用@自动填充用户,其中在线用户以绿色显示:

enter image description here

在同一行中,使用元数据和引导程序图标自动填充其他内容:

enter image description here

请分叉,拉动和改进!

答案 1 :(得分:1)