Iron router meteor auto unsubscribe?

时间秒杀一切 提交于 2019-12-24 02:33:29

问题


When using iron router to change to different template(pages), does it auto unsubscribe collection that no longer required ? Below scenario explain the question

  1. on page1, we call Meteor.subscribe(document, id)
  2. iron router change to page 2
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!