How to use Meteor methods inside of a template helper

前端 未结 6 1397
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 12:05

How can I define a Meteor method which is also callable in a template helper?

I have these two files:

file: lib/test.js

Meteor.methods({
             


        
6条回答
  •  旧巷少年郎
    2020-11-22 12:59

    You need to interface your return value with a Session variable as the request is asynchronous:

    Template.helloWorld.helpers({
        txt : function () {
            return Session.get("txt") || "Loading";
        }
    });
    
    Template.helloWorld.created = function() {
        Meteor.call('viewTest', 'Hello World.', function(err, result) {
            Session.set("txt", result);
        });
    
    }
    

    So .rendered should be called once when your template loads (at least it should with the newer version of Meteor.)

    The value would be called and displayed. Otherwise it would say "Loading".

提交回复
热议问题