流星Template.onRendered或Template.rendered是否使用jquery库? [英] Meteor Template.onRendered or Template.rendered for using a jquery library?
问题描述
我想将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
对应0
的setTimeout
,不在文档中.
通常,它可以解决您所依赖的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.
这篇关于流星Template.onRendered或Template.rendered是否使用jquery库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!