Reactive updates when a request is filtered by another request

I just started using a meteorite today and can't understand what I'm doing wrong. I have a query that runs inside a publish function, but this query is filtered by the result of another query.

In short, when I add a document to the database that is published (CollectionTwo), it works as I would expect, but when I make my change to the database that is used for filtering (CollectionOne), the meteor does not, t lead yourself reactive.

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 might be the appropriate solution?

+3
source share
3 answers

Meteor.publish . Meteor CollectionTwo.find CollectionOne.

, , , , . observe , CollectionOne, this.set this.unset . . , .

, .

+6

, , :

https://atmosphere.meteor.com/package/server-deps

https://atmosphere.meteor.com/package/reactive-publish

, "Meteor.reactivePublish" "Meteor.publish", , { "": .

readme , , .

Meteor.reactivePublish(null, function() {
  if (this.userId) {
    var user = Meteor.users.findOne({_id: this.userId}, {reactive: true});
    if (user.team) {
      var team = Collections.teams.findOne({_id: user.team}, {reactive: true});
      var visibleItems = _.compact(team.visibleItems);
      return Collections.items.find({_id: {$in: visibleItems}});
    }
  }
});
+4

reactive-publish ( ):

Meteor.publish "items", ->
  @autorun (computation) =>
    not_hidden = CollectionOne.find({}, {fields: my_id: 1}).fetch()
    CollectionTwo.find _id: $in: _.pluck not_hidden, 'my_id'

It is important to limit the requested fields from CollectionOneto my_id, otherwise it autorunwill be restarted when any field changes in documents CollectionOne, and not just my_id.

0
source

All Articles