Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how would you use subscription managers with meteor's template subscriptions?

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')
like image 573
funkyeah Avatar asked Apr 17 '15 21:04

funkyeah


2 Answers

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."

like image 129
Gaelan Avatar answered Oct 14 '22 08:10

Gaelan


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);
     }
   }
}
like image 21
funkyeah Avatar answered Oct 14 '22 08:10

funkyeah