I have a meteor template which displays comments. I've set up a reactive template helper which returns replies when they are added by users. I'd like to trigger a function when the number of replies changes--when any user adds a new reply to the current discussion), but I'm not sure what the best way to do this is.
Currently, I have this setup as part of the template helper, but this seems really brittle.
Template.comments.helpers({
replies: function() {
var discussionId = Session.get("openedDiscussion");
var replies = Replies.find({discussionId: discussionId});
// I want this function to run every time the # of replies changes.
foobar();
console.log('There are is a new reply from someone else');
return replies;
}
});
I've also tried using Deps.autorun, but can't figure out how to do this with properly with the Session object. I'm also unsure where to place this in my Meteor project:
Deps.autorun(function () {
var discussionId = Session.get("openedDiscussion");
var replies = Replies.find({discussionId: discussionId});
// I want this function to run every time the # of replies changes.
foobar();
console.log('There are is a new reply from someone else');
});
I also get this error in the console when I try to Uncaught ReferenceError: Replies is not defined