簡體   English   中英

流星方法不將html返回模板

[英]Meteor method not returning html to template

我正在嘗試為用戶首次登錄時顯示警告警告,首次登錄后我不希望顯示此消息。 所以我創建了這個方法:

Meteor.methods({
'firstLogin': function() {
    if (Meteor.user().loggedInTimes === 0) {
        return '<div class="alert alert-info alert-dismissible alertBar" role="alert"><br><button type="button" class="alertButton" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button><b>Please take a few minutes to personalise your experience at SSAW by updating your <a href="/profile" class="alert-link hoverAlert">profile</a></b>. </div>';
    }
}
});

我在Meteor.isClient稱為:

if (Meteor.isClient) {
Template.myHome.helpers({
    count: function(){
        var user = Meteor.user();
        //console.log(user);
        if (user) {
            Meteor.call('firstLogin');
        }
      }
  });
}

但是,這不會從方法中呈現任何html。 如何獲取html警報消息?

這是template

<template name="myHome">
<div class="container">
    {{{ count }}}
    <div class="text-center">
        {{#if currentUser }}
            <h1 class="roboto">Hi{{ username }}</h1>
        {{/if}}
    </div>
</div>

Meteor.call是一個異步函數,因此您將必須傳遞一個回調函數並返回結果。 這就是您需要更改的地方。

在這里,我將回調函數傳遞給Meteor.call ,返回結果或firstlogin方法出錯,如果沒有錯誤,則返回結果。

Meteor.call('firstLogin', function(error,result){
          if(!error){
              return result;
          }
    });

還可以使用Handlebars.SafeString從幫助函數返回html

完整的輔助功能

if (Meteor.isClient) {
  var count;

  Template.myHome.onCreated(function(){
    var user = Meteor.user();
        //console.log(user);
        if (user) {
            Meteor.call('firstLogin', function(error,result){
                if(!error){
                   count = result;
                }
        });
    }
  }

  Template.myHome.helpers({
    count: function(){
        return new Handlebars.SafeString(count);
    }
  });

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM