如何在另一个相关集合更改时重新订阅 Meteor 集合 [英] How to resubscribe to a Meteor collection when another related collection changes

查看:48
本文介绍了如何在另一个相关集合更改时重新订阅 Meteor 集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在创建新活动时重新订阅 ActivitySteps,否则用户无权访问为该活动新创建的步骤.我试过这个:

Activities.find().observeChanges {补充:->console.log '活动添加'Meteor.subscribe 'activitySteps'}

但是当重新订阅发生时,创建的活动似乎没有注册到发布功能.如果我尝试在其中设置 setTimeout 以将重新订阅延迟几毫秒,当然我会收到 Meteor 错误,提示我无法在模拟中设置计时器,但是它可以工作并且新步骤可用!

我也试过这个:

Deps.autorun ->Meteor.subscribe 'activitySteps', Activities.find().count()

哪个有效,即使在发布函数中不需要传入的计数值来确定当前用户应该可以使用哪些步骤.但这也会在 Activity 被删除时重新订阅,这似乎不是最好的方法.

我还在 Meteor 文档中看到可以从发布函数中观察更改,但这似乎相当复杂.

实现正确重新订阅的正确方法是什么?

解决方案

我相信通过将参数传递给发布函数应该可以解决问题.这在我当前的项目中对我有用.

服务器

Meteor.publish("活动", function () {返回活动.find({});});Meteor.publish("ActivitySteps", function (activityId) {返回 ActivitySteps.find({activityId: activityId});});

客户

Meteor.subscribe("活动");//我认为现在是 Deps.autorun 但我还没有那样使用它.Meteor.autorun(function () {Meteor.subscribe("ActivitySteps", Session.get("activityId"));});

I want to resubscribe to ActivitySteps whenever a new activity is created, otherwise the user doesn't have access to the newly created steps for that activity. I've tried this:

Activities.find().observeChanges {
  added: -> 
    console.log 'activity added'
    Meteor.subscribe 'activitySteps'
}

but it appears that the created activity isn't registered with the publish function when the resubscription happens. If I try to set a setTimeout inside there in order to delay the resubscription some milliseconds, of course I get a Meteor error saying I can't set a timer inside a simulation, but then it works and the new steps are available!

I've also tried this:

Deps.autorun ->
  Meteor.subscribe 'activitySteps', Activities.find().count()

which works, even though the passed-in count value isn't necessary in the publish function in order to determine which steps should be available to the current user. But this also resubscribes when an activity is deleted and seems like it might not be the best way to do it.

I also saw in the Meteor docs that it is possible to observe changes from within the publish function, but this seems rather complicated for that.

What is the right way to achieve the proper resubscription?

解决方案

I believe by passing a parameter to the publish function should do the trick. This has worked for me in my current project.

Server

Meteor.publish("Activities", function () {
    return Activities.find({});
});

Meteor.publish("ActivitySteps", function (activityId) {
    return ActivitySteps.find({activityId: activityId});
});

Client

Meteor.subscribe("Activities");
// I think this is now Deps.autorun but I haven't used it that way yet.
Meteor.autorun(function () {
    Meteor.subscribe("ActivitySteps", Session.get("activityId"));
});

这篇关于如何在另一个相关集合更改时重新订阅 Meteor 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆