简体   繁体   English

流星方法不将html返回模板

[英]Meteor method not returning html to template

I'm trying to show a alert warning for when a user first logs in, after their first login then I don't want this message shown. 我正在尝试为用户首次登录时显示警告警告,首次登录后我不希望显示此消息。 So I've created this method: 所以我创建了这个方法:

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

I call it in Meteor.isClient as: 我在Meteor.isClient称为:

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

However this does not render any of the html from the method. 但是,这不会从方法中呈现任何html。 How can I get the html alert message to appear? 如何获取html警报消息?

This is the template : 这是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 is an async function so You will have to pass a callback function and return the result. Meteor.call是一个异步函数,因此您将必须传递一个回调函数并返回结果。 This is what you need to change. 这就是您需要更改的地方。

Here i am passing a callback function to Meteor.call with returned result or error from firstlogin method and returning the result if there are no error. 在这里,我将回调函数传递给Meteor.call ,返回结果或firstlogin方法出错,如果没有错误,则返回结果。

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

Also Use Handlebars.SafeString to return html from helper function 还可以使用Handlebars.SafeString从帮助函数返回html

Complete Helper Function 完整的辅助功能

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