"Sharing" subscriptions, or nested subscriptions question

I apologize for the vague title, but my understanding of my own question is limited :relaxed:

I’m becoming more familiar in Meteor after developing a couple non-trivial apps and I have a question regarding subscriptions. I find myself becoming confused as to which templates have subscribed to what. For example, let’s say I have a Course collection with an accompanying CourseShow.html template that shows one course. This template subscribes to a course publication that takes the course’s id as an argument: Meteor.subscribe('course', courseId).

Now, within this CourseShow.html template, there are other sub-templates; let’s say Leaderboard.html. Leaderboard also needs to know some information about the Course that it is embedded in. Do I need to subscribe to the publication within Leaderboard.js again? Or is there a way to “share” this subscription? Even better, is there a way to share the actual Course object, so that I don’t need to call Courses.find().fetch()[0] again when I need access to course data?

I’ve read in the docs that you can do Template.parentData. Can I use that in some way to read data from the template that I’m embedded in? This seems a bit fragile though, because what if I embed the Leaderboard template onto a page that hasn’t subscribed to 'courses' :confused:

I’d be happy to provide more info. I realize this may be a little vague…

Thanks

Hi,
First you do not need to subscribe again. Subscribtion is an object that keeps some your client data synced with the server. While your subscribtion is still living all documents fetched by this sub are awailable on the client. On the other hand if you call Meteor.subscribe('course', courseId) twice with the same courseId Meteor is smart enough not to fetch data again!
Next. do not afraid of multiple Collection.find().fetch() (tip: you could use Collection.findOne()) inside helpers or autoruns. IMHO it is better than pass data context or get data context from parent. Get/Set context is more expensive and not so clear than find().fetch()

I used to put my subscriptions into IronRouter routes but I now find the code more maintainable if I put them into the templates created methods like this

Template.name.created = function() {
  this.subscribe('my_publication');
}

Some subscriptions can go into your layout templates created method which can be quite handy.

@mrzafod Great info, thank you! I didn’t know that Meteor was able to ignore duplicate subscription requests. That’s great to know because a lot of time I have more than one template needing the same data. Also good to know that using Collection.find().fetch() is a common pattern; I’ve been using it extensively.

@robsw I recently discovered this while reading a blog post! Template level subscriptions seem like a really good way to go for most use cases. Having the subscription automatically stopped upon template destruction seems like a great optimization. If you’re curious, here is the article I read.