13

Meteor recently introduced template subscription capabilities. You can now call this.subscribe from within a Temeplate.xyz.onCreated call and the helper {{#if Template.subscriptionsReady}} will only be true once the subscriptions have gotten ready from the server.

Unfortunately this does not seem to be obviously compatible with subs-manager or subs-Cache

How woudl you use subs-Cache in-place of this.subscribe such that the subscription ids made by the subsciptions manager make it into _subscriptionHandles and _allSubsReady part of this.subscribe? Or otherwise asked, how do you get {{#if Template.subscriptionsReady}} and the function Template.instance().subscriptionsReady() to depend on Template subscriptions made with subs-Cache.

Example code that does not work:

# in some top level file
share.subsCache = new SubsCache( 
  expireAter: 5  
  cacheLimit: 10
)

#in a template file
Template.entryRevisionInfo.onCreated ->
  share.subsCache.subscribe('somePub')
funkyeah
  • 3,074
  • 5
  • 28
  • 47

2 Answers2

3

The next (unreleased) version of meteor has a connection option to TemplateInstance#subscribe, and I would expect that you would be able to pass a subscription manager as the "connection."

Gaelan
  • 1,149
  • 11
  • 25
1

Sacha Greif wrote a solution in the Telescope app. I've tried to extract the parts that are significant to a basic implementation below. As far as I understand it relies on explicitly setting the ready status of the template... setting it reactively when the subscription is ready:

subsManager = new SubsManager();
Template.templatename.onCreated(function () {
   var instance = this;
   instance.ready = new ReactiveVar(false);
   subscription = subsManager.subscribe('yourCollection')
   instance.autorun(function () {
     if (subscription.ready()) {  //reactive
       instance.ready.set(true);
     }
   }
}
funkyeah
  • 3,074
  • 5
  • 28
  • 47
  • You can refer to https://www.discovermeteor.com/blog/template-level-subscriptions/ as well – Sacha Jul 22 '15 at 00:06