Question

As i understood from the docs, the Templates are rendered in a Deps autorun, and all state changed stop/subscribe will be maintained by it e.g. if abc.elements is called multiple times.

When the Template is removed from screen will then the subscriptions also be stopped or does i have to remove it manually in the Template.destroy method ?

[server.js]
Elements = new Meteor.Collection('Elements);
Meteor.publish('allElements', function() {
  this.onStop( function() {
     console.log('allElements.stop');
  });
  return Elements.find({});
});


[client.js]
Elements = new Meteor.Collection('Elements);
Template.abc.elements = function() {
  Meteor.subscribe('allElements);
  return Elements.find({});
}

[html]

<template name='abc'>
{{#each elements}}
...
{{/each}}
</template>
Was it helpful?

Solution

The subscriptions would still stay when you change/move away from a template. It's not entirely necessary to get rid of them. If you go back to the page the content will be ready faster.

If however you want to stop them you would put them in the destroy method. If you're using some kind of router it might be better to put it into one of the hooks so its ready before you visit the page.

It's not necessary to unsubscribe unless you're using a localized query in your publish which takes a param. e.g

Meteor.publish("data", function(page) {
    return Data.find({page:page});
});

In the above you would need to resubscribe to each page's content. But I suspect you're not doing this?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top