【发布时间】:2014-03-19 19:29:41
【问题描述】:
我有一个显示 cmets 的流星模板。我设置了一个响应式模板助手,当用户添加回复时,它会返回回复。我想在回复数量发生变化时触发一个函数——当任何用户向当前讨论添加新回复时),但我不确定最好的方法是什么。
目前,我将此设置作为模板帮助程序的一部分,但这似乎真的很脆弱。
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;
}
});
我也尝试过使用 Deps.autorun,但不知道如何正确使用 Session 对象。我也不确定在我的 Meteor 项目中放置它的位置:
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');
});
当我尝试 Uncaught ReferenceError: Replies is not defined 时,我也在控制台中收到此错误
【问题讨论】:
-
该文件中是否有回复?您是否将
Replies设为全局变量(没有var关键字?)。你确定你只在客户端运行它吗?
标签: javascript meteor