在DOM完成之前调用meteor blaze渲染
我有一个模板,显示了一个带有用户id的QRCode:在DOM完成之前调用meteor blaze渲染,meteor,meteor-blaze,Meteor,Meteor Blaze,我有一个模板,显示了一个带有用户id的QRCode: <template name="pairDevice"> {{#with currentUser}} <div id="qrcode"></div> <div class="key" id="qrcodeValue" style="display: none;">{{id}}</div>
<template name="pairDevice">
{{#with currentUser}}
<div id="qrcode"></div>
<div class="key" id="qrcodeValue" style="display: none;">{{id}}</div>
{{/with}}
</template>
我知道Blaze只渲染一次,但如何在DOM完成后让它进行渲染
谢谢将{{{with currentUser}}
移到模板之外
{{id}
{{#与当前用户}
{{>pairDevice}
{{/与}}
当您第一次刷新页面时,我相信
currentUser
在客户端使用其resume令牌进行身份验证时会在短时间内处于未定义状态。因此,当第一次呈现pairDevice
时,currentUser
未定义,因此调用rendered
回调时,两个div#qrcode
和#qrcodeValue
不存在。通过将with移出模板,在定义了currentUser
之前,不会呈现pairDevice
模板。如果将{{{with currentUser}}
移出模板,会发生什么?(因此,将其从pairDevice
中删除,然后使用{{{{with currentUser}}{{>pairDevice}{{/with}
调用pairDevice)它可以工作!你知道为什么吗?当你第一次刷新页面时,我相信currentUser
在短时间内是未定义的,而客户端使用其resume令牌进行身份验证。因此,当第一次呈现pairDevice
时,currentUser
未定义,因此最初不会呈现两个div#qrcode
和#qrcodeValue
。通过将与
一起移动到模板外部,在定义currentUser
之前,pairDevice
模板根本不会呈现。因为这样做有效,我将把它作为答案发布,以便您可以接受它。
Template.pairDevice.rendered = function(){
if (!location.origin) {
location.origin = location.protocol+"//"+location.host;
}
// $('#qrcode').qrcode({width : 128, height :128 ,text : $('#qrcodeValue').html()});
};
Template.pairDevice.helpers({
'id' : function(){
var appUser =Meteor.user();
var value = location.origin + ";" + appUser._id + ";" + appUser.emails[0].address;
$('#qrcode').qrcode({width : 128, height :128 ,text : value});
return value;
}
});