问题
When using iron router to change to different template(pages), does it auto unsubscribe collection that no longer required ? Below scenario explain the question
- on page1, we call Meteor.subscribe(document, id)
- iron router change to page 2
- on page 2 , we call Meteor.subscribe(document,id2) , does step 1 auto unsubscribe ?
回答1:
See here: https://github.com/EventedMind/iron-router/issues/265
Iron Router/Meteor does this for you: If you call Meteor.subscribe within a reactive computation, for example using Deps.autorun, the subscription will automatically be cancelled when the computation is invalidated or stopped;
If you wish to cache some of the subscription, see this excellent package: https://meteorhacks.com/subscription-manager-for-iron-router.html
this.route('postPage', {
path: '/post/:_id',
template: 'postPage',
waitOn: function() {
return Meteor.subscribe('post', this.params._id);
},
cache: 5, //cache 5 blog posts
expire: 3 //expire them if inactive for 3 minutes
});
回答2:
If you return the handle (or an array containing the handle) to the subscription in the route's waitOn
function, iron router will unsubscribe for you.
来源:https://stackoverflow.com/questions/25995399/iron-router-meteor-auto-unsubscribe