当查询被另一个查询过滤时的反应更新 [英] Reactive updates when query is filtered by another query

查看:46
本文介绍了当查询被另一个查询过滤时的反应更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天刚开始使用meteor,似乎无法弄清楚我做错了什么.我有一个在发布函数内运行的查询,但此查询由另一个查询的结果过滤.

I just started using meteor today and can't seem to figure out what I'm doing wrong. I have a query that is being run inside of a publish function, but this query is filtered by the result of another query.

简而言之,当我将文档添加到正在发布的数据库 (CollectionTwo) 时,它会按我的预期工作,但是当我在用于过滤的数据库 (CollectionOne) 中进行更改时,meteor 不会t 表现得被动.

In short, when I add a document to the database that is being published (CollectionTwo) it works as I would expect, but when I make my change in the database that is being used to filter (CollectionOne), meteor doesn't behave reactively.

CollectionOne = new Meteor.Collection("one")
CollectionTwo = new Meteor.Collection("two")

Meteor.publish("items", ->
  not_hidden = CollectionOne.find().fetch()
  return CollectionTwo.find( _id: {'$in':( t.my_id for t in not_hidden )} )
)

与此同时,在客户端...

Meanwhile, on the client...

CollectionOne = new Meteor.Collection("one")
CollectionTwo = new Meteor.Collection("two")

Meteor.subscribe("items")

_.extend( Template.items,
  items: ->
    not_hidden = CollectionOne.find().fetch()
    return CollectionTwo.find( _id: {'$in':( t.my_id for t in not_hidden )} )
)

您知道合适的解决方案是什么吗?

Any ideas what the appropriate solution might be?

推荐答案

反应性在服务器上的 Meteor.publish 中不起作用.当 CollectionOne 的内容发生变化时,Meteor 不会重新计算 CollectionTwo.find 查询.

Reactivity doesn't work that way inside Meteor.publish on the server. Meteor won't recalculate the CollectionTwo.find query when the contents of CollectionOne changes.

要实现您想要的,请手动管理发布,而不仅仅是返回 Cursor.您需要在发布函数中使用 observe 来观察 CollectionOne 上的变化,然后手动调用 this.setthis.unset 将更改推送到客户端.在发布文档中有此技术的示例.该示例仅查看一个集合,但您可以将此想法扩展到一组嵌套的观察.

To implement what you want, manage the publish by hand, instead of just returning a Cursor. You'll need to use observeinside your publish function to watch for changes on CollectionOne, and then manually call this.set and this.unset to push changes down to the client. There's an example of this technique in the publish documentation. The example only looks at one collection, but you can extend the idea to a nested set of observes.

我们将致力于让这种模式更容易实现.

We're going to work on sugar to make this sort of pattern easier to implement.

这篇关于当查询被另一个查询过滤时的反应更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆