Last active
September 22, 2016 01:39
-
-
Save SachaG/5828387 to your computer and use it in GitHub Desktop.
Use the return value of a Meteor method in a template helper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 thanks 👍
There is an error in the line 19, it must be Meteor.methods
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?
Is there a way to do this with reactive variables, or some template level scope?
How do you access the session on the template?
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
Here's a package that does this really well: https://atmospherejs.com/simple/reactive-method