Skip to content

Instantly share code, notes, and snippets.

@SachaG
Last active September 22, 2016 01:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SachaG/5828387 to your computer and use it in GitHub Desktop.
Save SachaG/5828387 to your computer and use it in GitHub Desktop.
Use the return value of a Meteor method in a template helper.
if(Meteor.isClient){
// set Session variable in method callback
Meteor.call('myMeteorMethod', "foo", function(error, result){
Session.set('myMethodResult', result);
});
// use reactive Session variable in helper
Template.myTemplate.helpers({
myHelper: function(){
return Session.get('myMethodResult'); // "bar"
}
});
}
if(Meteor.isServer){
Meteor.method({
myMeteorMethod: function(argument){
return "bar";
}
});
}
@ecwyne
Copy link

ecwyne commented Jun 23, 2015

Here's a package that does this really well: https://atmospherejs.com/simple/reactive-method

@vladholubiev
Copy link

@ecwyne thanks 👍

@aykutyaman
Copy link

There is an error in the line 19, it must be Meteor.methods

@merlinstardust
Copy link

I have similar code in my project but the result is always undefined. There's no error as I have a check for that and everything is working properly. But for some reason the result is undefined. Thoughts on why?

@brylie
Copy link

brylie commented Dec 10, 2015

Is there a way to do this with reactive variables, or some template level scope?

@Abdi4kilo
Copy link

How do you access the session on the template?

@bjornlll
Copy link

Thanks for the recommendation to try https://atmospherejs.com/simple/reactive-method @ecwyne, it works really well. The rest of you guys really should try it as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment