0

ユーザー ID の QRCode を表示するテンプレートがあります。

        <template name="pairDevice">
          {{#with currentUser}}
          <div id="qrcode"></div>
          <div class="key" id="qrcodeValue" style="display: none;">{{id}}</div>
          {{/with}}
        </template>

レンダリングされた値とヘルパーから値を設定しようとしましたが、フィールドがまだ存在しないため、常に同じ問題$('#qrcode')$('#qrcodeValue')返されます。[]

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;
  }
});

Blaze がレンダリングされるのは 1 回だけであることはわかっていますが、DOM の完了後にレンダリングするにはどうすればよいですか?

ありがとう

4

1 に答える 1

1

{{#with currentUser}}テンプレートの外側を移動します。

<template name="pairDevice">
  <div id="qrcode"></div>
  <div class="key" id="qrcodeValue" style="display: none;">{{id}}</div>
</template>

<!-- call it like this -->
{{#with currentUser}}
  {{> pairDevice}}
{{/with}}

ページを最初に更新するとcurrentUser、クライアントが再開トークンで認証している間、しばらくの間未定義であると思います。したがって、pairDevice最初にレンダリングされるときcurrentUserは未定義であるため、コールバックが呼び出されたときに2 つの div#qrcodeとは存在しません。with をテンプレートの外に移動すると、が定義されるまでテンプレートはまったくレンダリングされません。#qrcodeValuerenderedpairDevicecurrentUser

于 2014-05-19T09:42:08.590 に答える