Re-create template while switching routes


#1

Hello!
How can we re-create template while switching routes?

For example, i have subscriber template. It detects when user scrolls down to a display and subscribes to more data. It takes several parameters.

Example:
amazing_page.html

{{#each}}
   {{amazing_topic}}
{{/each}}

{{>subscriber name='topics' count=5}}

subscriber.js

//rough sample code     
Template.subscriber.onCreated(function() {
  var self = this;
  var type = Template.currentData().name;
  var count = Template.currentData().count;
  var user = Template.currentData().user;
  var skipCount = 0;
    self.autorun(function(c){
    self.subscribe(type, skipCount, user);
    var block = true;
    $(window).scroll(function() {
        if (($(window).scrollTop() + $(window).height()) >= ($(document).height()) && block) {
                block = false;
                skipCount = skipCount + count;
                console.log(type);
                console.log(skipCount);
                self.subscribe(type, skipCount, user, {
                    onReady: function() {
                        block = true;
                    },
                    onStop: function() {
                      console.log('stopped');
                    }
                });
        }
    });
  })
  });

I use this template with different parameters in different routes.
The problem is if user switches some routes, and scrolls down in one page, all subscribers he gets in another pages will actualy work in this page. More, they will store increased values for them variables, and will do all included logic.

I found a bad decision when we use Route.getName (for example) comparing and name parameter of subscriber. It is not a best option. Can someone help me to find a good practice for that?:slight_smile:

Simple Example:

We have 3 different routes:

1)News
2)Videos
3)Topics

These routes templates have included special subscriber-templates. And subscribtion works fine on scroll.

Ok, now let’s visit all of them: News, Videos, Topics.
Good, now scroll down and… I have three instance of subscriber template what will subscribe on them own publications, because they not destroyed when we switch routes.

And, as a result - when user scrolling Topics page, he will call subscribtion for News and Videos too, and he will take data from these collections too;)
And - this is a problem:)

UPD: Looks like i find a decision. If i use Template.instance (autorun/subscribe) it will start working expected, except some strange cases:)

First of all, when i go in another route in next iteration (scroll down) it returns me data from old, destroyed template + error. Next time (next iteration) it will start to subscribe to a correct data. Hmm…it looks like i have mistake in autorun section…or not?


#2

Can somebody help me with this strange behavior of Template.Subscribtion? :ghost: