【问题标题】:Meteor publish overwrites another publishMeteor 发布覆盖另一个发布
【发布时间】:2018-03-27 01:28:37
【问题描述】:

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

服务器

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")

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

【问题讨论】:

    标签: meteor meteor-publications


    【解决方案1】:

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

    客户端 - 搜索页面:

    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); 
    }
    

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

    【讨论】:

      【解决方案2】:

      欢迎来到 SO!

      您看到的“覆盖”很可能只是发布/订阅机制的正常 Meteor 行为。

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

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

      然后在您的客户端中,过滤来自TaskCollection 的一些文档独立于您的订阅和发布。只需执行你的TaskCollection.find({ state: state }) 客户端,你就会得到你需要的文件。

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

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

      另请参阅:Publish subscribe doesn't seem to work

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多