Call methods synchronously inside Tracker.autorun.
LAST UPDATED 10 APRIL 2019
Install with meteor add bartoftutormundi:reactive-method
Sometimes, you want to call a Meteor method inside of a template helper or Tracker.autorun computation, and get a return value. Now you can!
Template.foo.helpers({
methodResult: function () {
Meteor.call("myMethod", "a", "b", function (err, result) {
return result; // this doesn't work!!!
});
}
});
Template.foo.helpers({
methodResult: function () {
// Super fun!
return ReactiveMethod.call("myMethod", "a", "b");
// Can also use 'apply' style
// return ReactiveMethod.apply("myMethod", ["a", "b"]);
}
});
Be careful! If you pass something like Random.id()
or new Date()
as one of the arguments, you will cause an infinite loop where the method is called infinitely over and over. If you don't want your method call to re-run on every argument change, try this other package: https://atmospherejs.com/mnmtanish/call
Sometimes, you want to force a reactive method to get a new value from the server, even though the arguments are the same. In that case, use ReactiveMethod.invalidateCall
or ReactiveMethod.invalidateApply
, like so:
// Will cause the helper above to rerun
ReactiveMethod.invalidateCall("myMethod", "a", "b");