Javascript 使用订阅时,选择Meteor.js中未创建的2

Javascript 使用订阅时,选择Meteor.js中未创建的2,javascript,node.js,meteor,jquery-select2,select2,Javascript,Node.js,Meteor,Jquery Select2,Select2,好的-所以我在使用Select2和collections/subscriptions时遇到了一个小问题。如果我使用此.subscribe(集合),则不会创建select2,如果我注释掉订阅代码,则会创建select2。代码被包装在Template.subscriptionsReady块中,我在出版物中添加了Meteor.\u sleepForMs(2000),以检查模板是否等待,并且工作正常 还有其他人遇到过这个问题吗?谁知道如何修复它呢 HTML: <div class="input-g

好的-所以我在使用Select2和collections/subscriptions时遇到了一个小问题。如果我使用此.subscribe(集合),则不会创建select2,如果我注释掉订阅代码,则会创建select2。代码被包装在Template.subscriptionsReady块中,我在出版物中添加了Meteor.\u sleepForMs(2000),以检查模板是否等待,并且工作正常

还有其他人遇到过这个问题吗?谁知道如何修复它呢

HTML:

<div class="input-group">
    <span class="input-group-addon"></span>
    <select class="form-control select-group" style="width:100%" id="sector" name="sector" data-placeholder="Client sector">
        <option value=""></option>
        {{#each selectSectors}}
            <option value={{key}}>{{name}}</option>
        {{/each}}
    </select>
 </div

我在这里用完整的代码创建了jsfiddle-

onRendered
不应该在订阅准备就绪时再次触发。您的
select2
插件在数据准备就绪之前运行,当数据准备就绪时,Blaze会发现DOM与原来的不一样。您可以尝试使用助手提供的数据在子模板中呈现列表(顺便说一句,您没有包含该内部函数)。你可以从一个旧的包装中看到这个,以获得灵感。谢谢你。我将列表和订阅放在一个单独的模板中,然后在{{>templateName}中循环。这很有效。不确定这是不是正确的方法,但它满足了我的需要!
Template.createclient.onCreated(function() {
    this.subscribe("lkp_sectors"); // commenting this out allows the select2 to be created
});

Template.createclient.onRendered(function() {
    $(".select-group").select2();
});

Template.createclient.helpers({
    selectSectors() { return listSectors(); }
});