Meteor publish subscribe is not reactive

后端 未结 2 1018
日久生厌
日久生厌 2021-01-06 18:55

My client subscription routines are not refreshing when I update a collection:

server/publish.js

Meteor.publish(\'decisions\', funct         


        
2条回答
  •  醉梦人生
    2021-01-06 19:40

    The callback to Meteor.subscribe is called when the server marks the subscription as ready, and is not a reactive context, so it won't re-run when its dependencies change. (Reactive contexts aren't inherited like closure variables, the fact the callback is physically inside of the autorun is irrelevant.) You probably want a second autorun:

    // original autorun
    Deps.autorun(function() {
      var decSubscription = Meteor.subscribe("decisions", Number(Session.get('decisionCursor')));
      Meteor.subscribe("decisionsToModerate", Number(Session.get('decisionCursor')));
    
      // Store the subscription handle in a session variable so that
      // the new autorun re-runs if we resubscribe
      Session.set("decSubscription", decSubscription);
    });
    
    // new autorun
    Deps.autorun(function() {
      // don't do anything if the subscription isn't ready yet
      var decCursor = Session.get("decSubscription");
      if (!decCursor.ready()) {
        Session.set("decisionsVoted", {});
        return;
      }
      var decisionsVoted = {};
      Decisions.find({active: true}).forEach(/* ... */);
      Session.set("decisionsVoted", decisionsVoted);
    });
    

    Note that we skip computing decisionsVoted if the subscription isn't ready, otherwise when the server is sending the initial result set, we will recompute it after each individual document is sent.

提交回复
热议问题