meteor:模板级别的条件订阅

时间:2015-12-05 05:46:23

标签: javascript node.js mongodb meteor flow-router

我在具有不同数据参数的其他路径中重用相同的模板,但使用相同的出版物......

如果我执行普通的pub / sub,则数据按预期发布。但是当我像下面那样做条件发布/订阅时,我无法订阅数据。 console log return empty array ,,,

服务器/ publication.js

Meteor.publish('ACStats', function(cId, uId) {
    var selectors = {cId:cId, uId:uId};
    var options = {
        fields: {qId:0}
    };
    return ACStats.find(selectors,options);
});

的客户机/ onCreated

Template.channelList.onCreated(function() {
  this.disable = new ReactiveVar('');
  if (FlowRouter.getRouteName() === 'profile') {
    var self = this;
    self.autorun(function() {
      var penName = FlowRouter.getParam('penName');
      var u = Meteor.users.findOne({slugName:penName});
      if (u) {var uId = u._id;}
      Meteor.subscribe('ACStats', null, uId);
    });
  } else{
    var self = this;
    self.autorun(function() {
      var channelName = FlowRouter.getParam('channel');
      var c = Channels.findOne({title:channelName});
      if (c) {var cId = c._id;}
      Meteor.subscribe('ACStats', cId, null);
    });
  }
});

控制台

ACStats.find().fetch() //return empty array

任何人都弄清楚了我的错误.. ??

非常感谢你......

1 个答案:

答案 0 :(得分:0)

您可以制作两份出版物:

Meteor.publish ('ACStatsChannels', cId, function() {
});
Meteor.publish ('ACStatsUsers', uId, function() {
})

然后订阅如下:

Template.channelList.onCreated(function() {
   this.disable = new ReactiveVar('');
   var self = this;
   self.autorun(function() {
      if (FlowRouter.getRouteName() === 'profile') {
          var penName = FlowRouter.getParam('penName');
          self.subscribe('ACStatsUsers', penName);
      } else {
          var channelName = FlowRouter.getParam('channel');
          self.subscribe('ACStatsChannels', channelName);
    }
  });
});