Home >Web Front-end >JS Tutorial >How to Access Meteor Methods Within Template Helpers and Avoid Premature Rendering?
Problem:
How can you access a Meteor method within a template helper?
Solution:
You can face an issue where a template helper attempting to access a server-side method may fail because the template renders before the method can be defined. To resolve this, implement the following steps:
Updated Code:
<code class="javascript">// client/myView.js 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); }); }; Template.helloWorld.helpers({ txt: function () { return Template.instance().myAsyncValue.get(); }, });</code>
Additional Note: Ensure you add the reactive-var package using $ meteor add reactive-var for this solution to work.
The above is the detailed content of How to Access Meteor Methods Within Template Helpers and Avoid Premature Rendering?. For more information, please follow other related articles on the PHP Chinese website!