In Meteor, how can I request only the records for this subscription?

I understand that a subscription is a way to transfer records to a collection on the client side, from this message and others ...

However, for this post , you can have several signatures that come in one collection.

// server
Meteor.publish('posts-current-user', function publishFunction() {
  return BlogPosts.find({author: this.userId}, {sort: {date: -1}, limit: 10});
  // this.userId is provided by Meteor - http://docs.meteor.com/#publish_userId
}
Meteor.publish('posts-by-user', function publishFunction(who) {
  return BlogPosts.find({authorId: who._id}, {sort: {date: -1}, limit: 10});
}

// client
Meteor.subscribe('posts-current-user');
Meteor.subscribe('posts-by-user', someUser);

Now - I got my records through two different subscriptions, can I use the subscription to go to the records that she pulled out? Or should I complete my collection? What is the best practice for sharing this request between client and server?

, - , Meteor.subscribe , , , , . , - , , .

+4
3

, , . DDP, , , ( ) , .

, Meteor , , . :

if (Meteor.isServer) {
  Posts = new Mongo.Collection('posts');
}

if (Meteor.isClient) {
  MyPosts = new MongoCollection('my-posts');
  OtherPosts = new MongoCollection('other-posts');
}

if (Meteor.isServer) {
  Meteor.publish('my-posts', function() {
    if (!this.userId) throw new Meteor.Error();

    Mongo.Collection._publishCursor(Posts.find({
      userId: this.UserId
    }), this, 'my-posts');

    this.ready();
  });

  Meteor.publish('other-posts', function() {
    Mongo.Collection._publishCursor(Posts.find({
      userId: {
        $ne: this.userId
      }
    }), this, 'other-posts');

    this.ready();
  });
}

if (Meteor.isClient) {
  Meteor.subscribe('my-posts', function() {
    console.log(MyPosts.find().count());
  });

  Meteor.subscribe('other-posts', function() {
    console.log(OtherPosts.find().count());
  });
}
+8

, :

, BlogPosts Mongo 500 10 . :

Meteor.subscribe('posts-current-user'); // say that this has 50 documents
Meteor.subscribe('posts-by-user', someUser); // say that this has 100 documents

Meteor Meteor.subscribe('posts-current-user'); Mini-Mongo BlogPosts .

Meteor Meteor.subscribe('posts-by-user', someUser); someuser Mini-Mongo BlogPosts .

, Mini-Mongo BlogPosts 150 , 500 server-side BlogPosts.

, BlogPosts.find().fetch().count (Chrome Console), 150.

+2

! , . Iron Router, . .

But the general idea is that you connect a specific subscription to a specific template.

Template.onePost.helpers({
  post: function() {
    Meteor.subscribe('just-one-post', <id of post>);
    return Posts.findOne();
  }
});

Template.allPosts.helpers({
  posts: function() {
    Meteor.subscribe('all-posts');
    return Posts.find();
  }
));
0
source

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


All Articles