已发布集合的流星反应过滤

时间:2014-10-13 07:48:50

标签: javascript mongodb meteor

我有两个问题&模拟:

// Issues
{
  issueId: 1000, 
  appears: '2014-01-01',
  ...
}
// Simulations
{
  history: [50,200,30], 
  date: '2014-01-01',
  ..
}

如何重新发布不属于单一模拟的问题。

Meteor.publish('issues', function () {
    var simulation = Simulations.findOne({}),
        history = lodash.pluck(simulation.history, 'issueId');
    return Issues.find({
        issueId: {$nin: history},
        appears: {$lte: simulation.date}
    }, {limit: 12});
});

当我更改模拟日期或将issueIds推送到模拟历史记录中时,我希望发布的数据发生变化。 我找到了一些关系发布的包,但我很难理解如何使用它,这是我真正需要的。 https://atmospherejs.com/cottz/publish-with-relations

3 个答案:

答案 0 :(得分:1)

您的发布功能对参数进行反应性运行。我的意思是,一旦游标被发布函数返回,对该查询返回的记录集的更改将与客户端同步,但更改为查询< / em>不会有任何影响。因此,如果您要将另一个文档添加到与{em>原始查询Issues匹配的{issueId: {$nin: originalHistory}, appears: {$lte: originalSimulation.date}}集合中,它将被发送到客户端,但是如果您对{更改{ {1}}集合它将无效,因为发布函数已经返回了游标。

实现目标的一种方法是在自动运行块中订阅发布,并传递可根据需要更新的反应参数。将订阅保留在自动运行块中不仅会使其重新运行,而且会自动取消旧订阅,这样您就不会不断增加服务器负载。像这样:

服务器:

Simulations

客户端:

Meteor.publish('issues', function (simulation, history) {
    return Issues.find({
        issueId: {$nin: history},
        appears: {$lte: simulation.date}
    }, {limit: 12});
});

请注意,您可以通过观察更改并使用低级发布API来在服务器上实现此目的。有关如何执行此操作的更多信息here。以这种方式执行操作几乎肯定会更复杂,但取决于客户端和服务器上可用的计算容量,可能会有性能优势,

答案 1 :(得分:1)

我尝试过@richsilv方法,但最后我最终使用了https://atmospherejs.com/mrt/reactive-publish我不知道这是正确的方法,但由于它自动运行,现在就可以了。

Meteor.reactivePublish('issues', function () {
    var simulation = Simulations.findOne({}, {reactive: true}),
        history = lodash.pluck(simulation.history, 'issueId');
    return Issues.find({
        issueId: {$nin: history},
        appears: {$lte: simulation.date}
    }, {limit: 12});
});

答案 2 :(得分:1)

您可以使用reactive-publish包(我是作者之一)。它是@ slobodan.blazeski提到的包的重写:

Meteor.publish('issues', function () {
    this.autorun(function (computation) {
        var simulation = Simulations.findOne({}, {fields: {history: 1, date: 1}}),
            history = lodash.pluck(simulation.history, 'issueId');
        return Issues.find({
            issueId: {$nin: history},
            appears: {$lte: simulation.date}
        }, {limit: 12});
    });
});

重要的是,您要限制您对模拟文档感兴趣的字段,以便在其他字段更改时autorun不会不必要地重新运行。

相关问题