2015-11-06 20 views
1

我試圖在用戶首次登錄後顯示警告警告,然後我不希望顯示此消息。所以我創造了這個方法:Meteor方法不會將html返回到模板

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> 

回答

1

Meteor.call是一個異步功能,所以你將不得不通過一個回調函數,並返回結果。這是你需要改變的。

我在這裏從firstlogin方法傳遞一個回調函數Meteor.call與返回的結果或錯誤,如果沒有錯誤的結果返回。

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

} 
+0

感謝.....的HTML是不會出現在模板,但我確實看到它在console.log()中輸出。 – user94628

+0

@ user94628我可以看到您的模板代碼 – Nakib

+0

只需在問題中添加模板代碼。 – user94628