Accessing parent data in MeteorJS

January 09, 2014

Reading time ~1 minute

I had some problems finding out how to access parent data in MeteorJS. Consider the following code:

The collection we have for this look like this:

As we iterate over cars, this will be set to the current car object. But in order to remove the car from the Garage collection, we will need the _id of the parent garage. The parent data is accessible in the template event handler. The events take an event map, and in the case of events in the template event handler you get two arguments; event and template. The template refers to template where the handler is defined (i.e. the parent template). This is the key. So to implement the removeCar button, write like this:

Note how we extract the parent ID from template.data._id.