How can we isolate subscribtions data? [solved]

How can we isolate subscribtions data between templates?

For example - i have one page with two different template:

  1. Topic list

  2. Popular topics.

I have two different Meteor.publish and Subscribtions for this.

  1. In Topic-list template i have sorted by CreatedAt-field.

    Meteor.subscribe(‘topics’);

       Template.topics_main.helpers({
             topics:function(){
                 return Topic.find({},{sort: {createdAt: -1}});
             }
       });
    
  2. In Popular list i sort data by Rating-field.

    Meteor.subscribe(‘popularTopics’);
    Template.top_topics.helpers({
    topics:function(){
    return Topic.find({}, {
    sort: {
    views: -1
    },
    limit: 5
    });

    }
    

    });

And when i will scroll my Topic-list, i will get data from Popular Topics. This is not good:) How can i isolate data beetwen two templates with diferent subscribtions, but one type of collections?

Use template subscriptions. They improve isolation by being properly torn down when the template is destroyed:

Template.someTemplate.onCreated(function() {
  this.subscribe('someSubscription');
  // ...
});

This can always happen (even if only momentarily) due to the way that the merge box works. The answer is to always use the same find query in the template helper that you used in your publication.

1 Like

Hello @robfallows! Ty for reply!
Actualy i use template level subscribtions.
I build a simple example about my question. Look at the picture #PaintingTime #yeeeah
I have one (for example) Topic_main template, it consider Topic_list and Top_topic template
Top_topic have subscribtion in onCreated block;
Topic_list consider {{scroll-subscriber}} template, builded by my own.
In theory, these two templates isolated, in fact-no.

I cant specify Find query in this case, cause it will not solve the problem.

P.S. If i will scroll more, my subscriber will load more data, and everything will fall into place

Can you show your publications?

Hello @lucfranken! Of course !

Sample:

Topics_List

return Topic.find({}, {
                sort: {
                    createdAt: -1
                },
                limit: 5,
                skip: skipCount
            });

Top Topics:

return Topic.find({}, {
                sort: {
                    views: -1
                },
                limit: 5
            });

This is kind of just the way Meteor works - by default, just puts all of the published documents into a collection on the client with the same name, and any filtering needs to be done again. But with pagination/skipping this doesn’t really work well.

In this case you can try using this package, which automatically tags documents with the publication for you, so that the data doesn’t overlap: https://atmospherejs.com/percolate/find-from-publication

3 Likes

Hey @sashko!
Thank you, it looks like good decision.

1 Like