繁体   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