Subscription to a source of reactive data using autorun

I am trying to write webapp using Meteor, and I definitely cannot understand something about subscribing to published datasets. The whole application is on github (related to the last commit for posterity), but I will try to summarize below.

I have a collection called commands, available for both client and server:

Teams = new Meteor.Collection( "teams" ); 

On the server, I want to publish a list of all the commands:

 Meteor.publish( "allteams", function() { ... 

There is a very simple cursor that makes up this published list:

 var handle = Teams.find( {} ).observeChanges({ added: function( id ) { console.log( "New team added" ); if ( !initializing ) { console.log( "Telling subscribers it all change" ); self.added( "teams", id, {} ); self.ready(); } } }); 

The client subscribes to this source, and when elements are added, the client will add pins to the map:

 Meteor.autorun( function() { Meteor.subscribe( "allteams", function() { console.log( "All teams has been updated" ); // Do more stuff } }; 

When the list is initially populated, autostart works fine, but if I add another item to the collection, the publisher method writes “I noticed this,” but nothing happens in the subscriber.

The purpose of the foregoing is to:

  • The server has a list of commands consisting of a name and a long / part detail.
  • When a client connects, they receive this list of commands and they are displayed on the map.
  • If the command is added to the list on the server side, each client is notified and a new contact appears on the map.

In terms of the app, I probably don't need the pins to magically appear, but this is a useful way to teach publishing and subscribing, especially when I don't understand! In the end, "allteams" is likely to be a little smaller than the entire list of commands, so I think it is like looking at the data.

Am I missing something completely obvious?

Edit: I worked through and put the answer below. tl; dr I did not subscribe to the source of reactive data at all.

+4
source share
2 answers

He may not be polite to answer my own question, but I found out what I was doing wrong and decided that other people might run into the same thing.

The simple answer is that I did not do what I claimed to do in the name of the quests, in particular, did not subscribe to a reactive data source.

 Meteor.autorun( function() { Meteor.subscribe( "allteams", function() { console.log( "All teams has been updated" ); // Do more stuff } }; 

Here I passed the subscribe autorun method, but this method itself is not a reactive data source. However, it returns something that is!

 // Define a subscription var handle = Meteor.subscribe( "foo", { onReady: function() { ... } } ); Meteor.autorun( function() { if ( handle.ready() ) { // Now do something every time the subscription is marked as ready } }; 

The ready method of the subscription descriptor is reactive, so autorun now runs every time an updated set of documents is updated. This leads me to additional questions about the effectiveness of several clients subscribing to the database cursor, and monitoring changes, but I will come to this in another question.

+13
source

If auto-update is still enabled, this may be the cause of your problems. Try disabling autorun and see if the publish instruction works correctly.

0
source

Source: https://habr.com/ru/post/1469076/


All Articles