【问题标题】:Meteor Template helpers: {{keyname}} showing blank流星模板助手:{{keyname}} 显示空白
【发布时间】:2015-06-11 16:41:31
【问题描述】:

comment.html:

<template name="comment">
    <img src="{{photo}}"> //blank
    {{photo}} //blank
</template>

cmets.js:

Template.comment.helpers({
    photo: function(){
        Meteor.call('getPhoto', function(error, result) {console.log(result);return result})
    }
});

server.js:

Meteor.methods({
    getPhoto: function () {
        return Meteor.user().services.vk.photo;
        }
});

问题: console.log 返回正确的值,但 {{photo}} 为空。 问题:为什么“照片”是空的?

【问题讨论】:

    标签: javascript meteor handlebars.js meteor-blaze


    【解决方案1】:

    [更新]

    我才意识到这里有什么问题。

    Meteor.call 正在调用异步函数,例如 ajax 调用。所以Meteor.call('getPhoto') 会返回 undefined,结果只能在回调中获取

    Meteor.call('getPhoto',function(err,result){console.log(result)});
    

    考虑到这一点,您需要想出一种方法来捕获回调中的结果。一种解决方案是使用ReactiveVariable:

    您首先需要$ meteor add reactive-var

    Template.comment.created = function (){
        var $this = this;
        $this.photo = new ReactiveVar("loading");
        Meteor.call('getPhoto', function (err, result) {
            if (err) console.log(err);
            $this.photo.set(result);
        });
    }
    

    现在定义你的助手来获取值;

       //this is optional
       Template.comment.helpers({
            photo: function(){ 
               return Template.instance().photo.get();
            }
        });
    

    另一种解决方案是使用Session:

      //everything here is in the client
      Meteor.call('getPhoto', function(error, result){
        Session.set('thePhoto', result);
      });
    
      // use reactive Session variable in helper
      Template.comment.helpers({
        photo: function(){
          return Session.get('thePhoto');
        }
      });
    

    使用Session 的问题在于您正在设置一个全局变量,如果您有很多 cmets,并且每条评论都需要有一张独特的照片,Session 可能不是最好的方法。


    您在声明助手时调用了函数Meteor.call

    Template.comment.helpers({
        photo: Meteor.call('getPhoto', function(error, result) {console.log(result);return result})
    
    });
    

    所以你正在做的相当于:

    var a = Meteor.call('getPhoto', function(error, result) {console.log(result);return result})
    Template.comment.helpers({
            photo: a //a is just a value 
    });
    

    要使.helpers 正常工作,您应该为photo 分配一个函数。

    Template.comment.helpers({
        photo: function(){
           var r;
           Meteor.call('getPhoto', function(error, result) {r = result});
           return r;
        } 
    });
    

    在底层,每个助手都会启动一个新的 Tracker.autorun。当它的反应性依赖发生变化时,助手会重新运行。助手依赖于他们的数据上下文、传递的参数和其他在执行期间访问的反应数据源。 -来自 Meteor Doc

    .helpers 应该被调用,因为您要使用.helpers 的原因是在您的视图中启用reactivity。因此,.helpers 内部的内容必须是函数。


    如果你还是不明白我的意思,这里是简化的例子:

    var a = function(){ console.log("hey"); return 1}
    
    var b = a();
    
    var c = a; 
    
    b(); //this would run into an error saying that b is a not a function
    
    c(); //this would console.log "hey"
    

    【讨论】:

    • 谢谢你的回答。我已经修改了comment.js文件,但没有任何改变;(
    • @bartezr 它记录了任何结果吗?这可能是由于您的&lt;template name="comment&gt; 中缺少" 造成的吗?
    • @bartezr 我刚刚发现了这个问题;给我一分钟;
    • @bartezr 刚刚更新了我的帖子;经测试;它现在肯定可以工作了。
    • 现在它运作良好。感谢您的帮助,很抱歉浪费您的时间。
    猜你喜欢
    • 2015-04-15
    • 1970-01-01
    • 2016-12-07
    • 2015-09-05
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多