Search code examples
meteormeteor-helper

Get value returned from another method


In a Template helper, is it possible to get from a method a value returned by another method?

In example

Template.postsList.helpers({
  posts: function () {
    return Posts.find({});
  },
  nextPath: function () {
    // how to return here the number of posts from the query
    // in the posts method?
  }
});

Solution

  • You can just refactor the code so you have a shared way to obtain the posts cursor:

    var postsCursor = function() {
      return Posts.find();
    };
    
    Template.postsList.helpers
      posts: postsCursor,
      nextPath: function () {
        var count = postsCursor().count();
        // do something with count
      }
    });