I like the dburles:collection-helpers package to add structure to collections
however when using template based subscriptions, collection helper functions are getting triggered before the entire subscription is ready , leading to exceptions.
How can I avoid this and run collection helpers when all subscriptions are ready ?
I am also using reywood:publish-composite could that be a reason for missing data when collection helpers run ?
Inside the template’s HTML, you can use the built-in helper Template.subscriptionsReady, which is an easy pattern for showing loading indicators in your templates when they depend on data loaded from subscriptions. http://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe
Maybe my question was not very clear.
The issue is not with Template Helpers
But Collection helpers as enabled with dburles:collection-helpers package. this helpers do not belong to any specific templates.
Since data is reactive and it is not always available on the client (before the subscriptions are ready at least) you should use protective property checks in your javascript. In your case, since childObj is not always there, childObj.slug is therefore not always an existing property, therefore is sometimes undefined. Here’s what you could do:
ParentCol.helpers({
relatedChild : function (){
return ChildCol.findOne({domain: this.domain},{fields: {title:1,image:1,slug:1}});
},
childPage : function (){
var childObj = ChildCol.findOne({domain: this.domain},{fields: {slug:1}});
// First check if we actually have a childObj
return childObj && FlowRouter.path('childPage', {slug: childObj.slug});
}
Publish composite is just a utility that lets you publish from multiple collections.
Subscribe is a mechanism where you connect to a publication for the data you need.
Where you need the data is irrelevant of router/template disinction.
On the contrary, it is still a very good idea to keep the subscriptionin the template, where the data is actually used.
I’ve been using publish composite, collection hooks, collection helpers, collection2, simple schema together extensively and it fits very naturally with template subscription.