Template-level subscriptions
This pattern attaches the Meteor.subscribe
functions to templates. The key advantage of subscribing from the template is the ability to isolate the template and know that it still works when it is rendered.
Many Meteor developers attach their subscription methods to their routes. This means that the template will only render with the correct data at that particular route.
With this pattern, we will be able to reuse templates anywhere without worrying about data.
Setting up products for the online shop
Let's start by setting up a Products
collection in MongoDB for our online_shop
project. In Chapter 1, Getting Started with Meteor we learned that we need to place this definition under the /globals/lib/collections
directory:
# /globals/lib/collections/products/products.coffee
@Products = new Mongo.Collection "products"
# fields:
# name
# description
# sku
It's important to note that we are adding @
at the beginning of the Products
variable. This compiles into this...