在 Meteor 0.7.0.1 中,是否可以计算/找出当前正在监听特定 Collection 的所有查询?
我正在尝试创建一个函数,该函数执行以下操作:每当监听特定查询的用户数量(例如:myCollection.find({color:'red'})
变为非零时,每当文档被更改/添加到第二个集合anotherCollection
时执行函数。
最佳答案
何时/如何调用 find 方法?例如,如果当有人点击页面上的按钮时调用它,只需增加一个服务器端变量,该变量在发生这种情况时就会增加。要在用户离开页面时减少此变量,请监听 window.onbeforeunload
事件并在发生该事件时减少计数。
或者,如果您有登录系统,请为每个用户分配一个 bool 值,例如 online
。当他们登录时,使用以下代码使他们的在线状态正确。 if(Meteor.user()){Meteor.user().online=true;}
。确保在他们离开时 onbeforeunload
将其在线状态设置为 false。然后,执行诸如 Meteor.users.find({online:true}).size()
之类的操作来获取在线用户数量。
基本上,不要在调用 myCollection.find({color:'red'})
时进行更新,而是将其放入函数中。例如:
if(Meteor.isClient){
Session.set('browsing', false);
function findColor(c){//Alerts the server when the user attempts to get a color.
//This is presuming they don't use the plain MongoDB command.
//Your button/however you're finding the color should use this command
if(!Session.get('browsing'))//if it's already true, don't increase the count
{
Meteor.call('incBrowsing');
Session.set('browsing', true);
}
return myCollection.find({color:c});
}
window.onbeforeunload = function(){Meteor.call('decBrowsing');};
}
if(Meteor.isServer){
var browsing = 0;
Meteor.methods({
incBrowsing: function(){browsing++;},
decBrowsing: function(){browsing++;}
});
}
我还没有测试过这个,但我希望它对你有用。您没有提供有关问题的太多详细信息,因此如果您或我需要澄清某些内容,请随时在下面发表评论。
关于meteor - 找出 Meteor.js 集合上监听的所有查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21036802/