查询由另一个查询过滤时的反应性更新

bry*_*sgo 3 meteor

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

简而言之,当我将文档添加到正在发布的数据库(CollectionTwo)时,它可以像我期望的那样工作,但是当我在用于过滤(CollectionOne)的数据库中进行更改时,流星不会表现出反应性.

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 )} )
)
Run Code Online (Sandbox Code Playgroud)

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

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 )} )
)
Run Code Online (Sandbox Code Playgroud)

任何想法适当的解决方案可能是什么?

deb*_*lis 6

反应性Meteor.publish在服务器上不起作用.CollectionTwo.findCollectionOne更改内容时,Meteor不会重新计算查询.

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

我们将开始研究糖,以使这种模式更容易实现.