【发布时间】:2015-04-03 19:41:14
【问题描述】:
我有一个应用程序,用户可以在其中管理多个警报(设置新警报、编辑或暂停)
当到达闹钟时间时,我需要通知用户。如果用户点击贪睡,time_end 字段会增加 5 分钟。
我如何跟踪到达闹钟时间的时间?
我尝试过使用collection.observe() - 但它只能在服务器启动时工作一次
服务器:Meteor 启动
var expiredAlarms = Alarms.find({$and: [{"time_end": {$lt: moment()._d}}, {notification_sent: false}]});
expiredAlarms.observe({
added: function(doc) {
console.log('alarm timeout has been reached');
processAlarmEnd(); //set notification_sent to true
},
removed: function(doc) {
console.log('A notification has been sent');
}
});
上述代码仅在应用程序启动并处理过期警报的所有通知时有效 - 但一旦新警报过期,则不会发生任何事情。我的猜测是因为moment()._d 没有改变,并且一遍又一遍地调用相同的旧查询。
我应该将此代码放在Tracker.autorun 中吗?这有帮助吗?关于做得更好/更智能的任何建议?
【问题讨论】:
标签: javascript meteor collections mongodb-query dom-events