Meteor.call from Handlebars Block Helper

I am trying to use the Meteor.call function inside the Handlebars block helper

 Handlebars.registerHelper('get_handle', function(profileId, name) { Meteor.call("getProfileLink", profileId, function(error, result) { if (error) { return new Handlebars.SafeString('<a href="#">' + name + '</a>'); } else { return new Handlebars.SafeString('<a href="http://twitter.com/' + result + '">' + name + '</a>'); } }); }); 

In console.log(result) I see that the result is returned, but the HTML from this helper is not displayed. However, when I return the value of Handlebars.SafeString from Meteor.call , it works fine. What am I doing wrong here? Or is it wrong to use Meteor.call in the Handlebars block?

+4
source share
1 answer

You cannot use Meteor.call in the handlebars block with the paradigm above, primarily due to the asynchronous javascript design, by the time the value is received from the server, the return value has already been returned.

However, you can pass it using the Session variable:

 Handlebars.registerHelper('get_handle', profileId, name, function() { return new Handlebars.SafeString(Session.get("get_handle" + profileId + "_" + name)); }); //In a meteor.startup or a template.render Meteor.call("getProfileLink", profileId, name, function(error, result) { if (error) { Session.set("get_handle" + profileId + "_" + name, '<a href="#">' + name + '</a>'); } else { Session.set("get_handle" + profileId + "_" + name, '<a href="http://twitter.com/' + result + '">' + name + '</a>'); } }); 

Also, be careful when trying to use to have so many Meteor.call for each profileId and name (if you use it in some list or something else) when you can request data in one bulk request.

Hacker way

You can still do it as you plan, but I would advise you to do it. I think this is a little inefficient.

 Handlebars.registerHelper('get_handle', profileId, name, function() { if(Session.get("get_handle" + profileId + "_" + name)) { return new Handlebars.SafeString(Session.get("get_handle" + profileId + "_" + name)); } else { Meteor.call("getProfileLink", profileId, name, function(error, result) { if (error) { Session.set("get_handle" + profileId + "_" + name, '<a href="#">' + name + '</a>'); } else { Session.set("get_handle" + profileId + "_" + name, '<a href="http://twitter.com/' + result + '">' + name + '</a>'); } }); return "Loading..." } }); 
+4
source

All Articles