流星发布覆盖另一个发布

Meteor publish overwrites another publish

我有如下两种发布方法,但是当我订阅客户端搜索页面中的一种发布方法时,它会被另一种用于索引页面的方法覆盖。

服务器

Meteor.publish("task.index", function() {
  TaskCollection.find()
}

Meteor.publish("task.index.search", function(state) {
  TaskCollection.find({ state: state })
}

客户端 - 搜索页面

Meteor.subscribe("task.index.search", state)
// this will always be overwritten with "task.index" published collection

客户端 - 索引页

Meteor.subscribe("task.index")

有谁知道如何避免这种情况?

欢迎来到 SO!

您看到的 "override" 很可能只是 Publish/Subscribe 机制的正常 Meteor 行为。

您的 "task.index" 出版物将 所有 您的 TaskCollection 文档发送给客户。

因此,同一 TaskCollection 上的任何其他出版物将发送客户已经知道的文档。

然后在您的客户端中,从 TaskCollection 中过滤一些文档与您的订阅和发布 无关 。只需执行您的 TaskCollection.find({ state: state }) 客户端,您将获得所需的文件。

当您只发布一个集合的文档子集时,您发布的内容 恰好 已经是您要在客户端上显示的过滤文档,因此在您的客户只需显示您知道的所有收集文件。但是你必须明白这是两个不同的步骤:

  1. 订阅发送一些文件给客户。可以设置多个订阅,填充客户端上的相同集合。
  2. 根据(可能是多个)订阅发送的文档在客户端上进行过滤。

另请参阅:

如果您的客户端索引和搜索页面是不同的模板,您可以订阅各自模板级别的文档。

客户端-搜索页面:

Template.search.created = function () {
    const template = this;
    template.subscribe('task.index.search', state);
}
Template.search.rendered = function () {
    console.log("Client search : " + TaskCollection.find().fetch().length); 
}

客户端 - 索引页:

Template.index.created = function () {
    const template = this;
    template.subscribe('task.index');
}
Template.index.rendered = function () {
    console.log(""Index : " + TaskCollection.find().fetch().length); 
}

但是,始终建议在客户端也过滤文档。