How to use Meteor methods inside of a template helper

前端 未结 6 1369
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 12:05

How can I define a Meteor method which is also callable in a template helper?

I have these two files:

file: lib/test.js

Meteor.methods({
             


        
6条回答
  •  温柔的废话
    2020-11-22 12:53

    Sashko added a neat little package called meteor-reactive-method to solve this problem.

    $ meteor add simple:reactive-method
    
    Template.helloWorld.helpers({
      txt: function() {
        return ReactiveMethod.call('viewTest', 'Hello World.');
      }
    });
    

    As I point out in common mistakes, helpers should be side-effect free, so I'd use this technique with caution. However, it's a really handy shortcut for cases where:

    • The helper should fire only once (it doesn't depend on reactive state).
    • The invoked method doesn't mutate the database.

提交回复
热议问题