How to Use Meteor Methods Inside of a Template Helper

How to use Meteor methods inside of a template helper

There is now a new way to do this (Meteor 0.9.3.1) which doesn't pollute the Session namespace

Template.helloWorld.helpers({
txt: function () {
return Template.instance().myAsyncValue.get();
}
});

Template.helloWorld.created = function (){
var self = this;
self.myAsyncValue = new ReactiveVar("Waiting for response from server...");
Meteor.call('getAsyncValue', function (err, asyncValue) {
if (err)
console.log(err);
else
self.myAsyncValue.set(asyncValue);
});
}

In the 'created' callback, you create a new instance of a ReactiveVariable (see docs) and attach it to the template instance.

You then call your method and when the callback fires, you attach the returned value to the reactive variable.

You can then set up your helper to return the value of the reactive variable (which is attached to the template instance now), and it will rerun when the method returns.

But note you'll have to add the reactive-var package for it to work

$ meteor add reactive-var

Meteor - How call another function in same template helper

There is a trick that you can use meteor execute the functions call from right to left so your one function output will be because input for the another function and so on. I hope that make sense to you.

Your html code

<template name="hello">
{{getDaysInParticularMonth getDaysInMonth}}
</template>

Your js code

Template.hello.helpers({
getDaysInMonth: function(){
var now = new Date();
return [now.getMonth(), now.getFullYear()];
},
getDaysInParticularMonth: function(array) {
console.log("hey");
return 0; //just for test
},
});

But if you want to to just call a function from the helper then you have to define the function outside of helper block this is how you can do that as well.

In my html file:

<template name="hello">
{{getDaysInMonth}}
</template>

In js file:

Template.hello.helpers({
getDaysInMonth: function(){
var now = new Date();
return getDaysInParticularMonth(now.getMonth(), now.getFullYear());
},

});

function getDaysInParticularMonth(month, year) {
console.log("hey");
return 0; //just for test
},

Using a Meteor function inside of a Template helper gives error

The core problem here is the same as in this question. Using strings as data contexts (in this case iterating over an array of usernames) results in some weird corner cases, because each will be converted into a String instance. The fix that requires the fewest number of changes is just to call toString in both situations where you manipulate the Session variable:

var isSelected = Session.equals('selectedFriend', this.toString());

and

Session.set('selectedFriend', this.toString());

That will give you a string primitive instead of a String object, which Session can readily use.

As an aside, I feel compelled to point out that using ids instead of usernames is generally a safer bet - an _id will never change, but if the username does, your association will break.

Meteor.js - Calling a template helper within template rendered

This isn't really the right way of going about things as the way Meteor works is by compiling templates before the application starts, rather than at run-time. Whilst something along these lines may be possible (for example by using Template.registerHelper), it would be much better to set a reactive variable to a specific value in the rendered callback and have the helper set to return that instead:

Session.setDefault('randomNum', 0);

Template.myTemplate.rendered = function () {
Session.set('randomNum', Math.random());
}

Template.otherTemplate.helpers({
randomNum: Session.get('randomNum')
});

If you'd rather use a private variable for the randomNum, have a look at ReactiveVar. It could be any reactive data source and it would work.



Related Topics



Leave a reply



Submit