我有一个用于平板电脑的客户端应用程序和一个用于笔记本的应用程序这些应用程序位于客户端文件夹中:分别为“平板电脑”和“监视器”。我的问题是,是否可以触发从平板电脑应用程序到笔记本应用程序的事件?因为我想同步切换分页。
答案 0 :(得分:0)
Meteor中的典型模式是保存或更新两个客户订阅的集合中的文档,然后让客户端对该新值或更新值做出反应。
例如:
LIB:
SharedState = new Mongo.Collection('sharedstate');
服务器:
Meteor.publish('shared',function(){
return SharedState.find({ userId: this.userId });
});
客户端:
Meteor.subscribe('shared');
在您创建活动的客户端中:
SharedState.upsert(
{ userId: Meteor.userId() },
{ userId: Meteor.userId(), state: "foo" }
);
然后在正在查找事件的客户端中使用Tracker.autorun(或者observeChanges)来查看共享状态是否已更改:
Tracker.autorun(function(){
let state = SharedState.findOne({ userId: Meteor.userId() });
if ( state === "foo" ){
... do your thing
}
});
这里我假设你的平板电脑和笔记本电脑上运行的应用程序正被同一个用户使用 - 因此共享状态的关键是userId
。如果OTOH你在教室(一位老师,几位学生)分享状态,那么你可能会使用类似classroomId
的东西。