Reputation: 2745
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?
Upvotes: 1
Views: 1154
Reputation: 362
I believe by passing a parameter to the publish function should do the trick. This has worked for me in my current project.
Meteor.publish("Activities", function () {
return Activities.find({});
});
Meteor.publish("ActivitySteps", function (activityId) {
return ActivitySteps.find({activityId: activityId});
});
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"));
});
Upvotes: 2