流星Template.onRendered或Template.rendered是否使用jquery库? [英] Meteor Template.onRendered or Template.rendered for using a jquery library?

查看:70
本文介绍了流星Template.onRendered或Template.rendered是否使用jquery库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将selected(一个jquery库)与流星一起使用,我只需要使用以下代码:

I want to use chosen(a jquery library) with meteor and I just need to use this code:

    $('#ship').chosen();

我尝试使用.onRendered,但是如果我希望它可以工作,我需要等待

I tried using .onRendered but I need to wait, if I want it to work

Template.createTradeForm.onRendered(function(){
    //Strange bug, need to wait here or it doesn't work..
    setTimeout(function(){
        $('#ship').chosen();
    }, 2000);
});

此解决方案存在相同的问题:

Same problem with this solution:

Template.createTradeForm.rendered = function(){
    //here again, I need to wait or it doesn't work
    setTimeout(function(){
        $('#ship').chosen();
    }, 2000);
};

这个问题还有其他解决方案吗?这个setTimeout在这里不是很好.

Is there any other solutions to this problem? This setTimeout isn't really good here.

修改 我的助手按要求

Template.createTradeForm.helpers({
    'getShips': function(){
        return Ship.find()
    }
});

推荐答案

将代码包装在Meteor.defer内,如下所示:

wrap your code inside Meteor.defer, like this:

Template.createTradeForm.onRendered(function(){
    Meteor.defer(function(){
        $('#ship').chosen();
    });
});

Meteor.defer对应0setTimeout,不在文档中. 通常,它可以解决您所依赖的DOM中的某些内容尚未呈现的情况.

Meteor.defer corresponds a setTimeout of 0 and it's not in docs. It typically solve cases where something in the DOM that you're depending on has not yet rendered.

一些参考: 1 查看全文

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆