Usually I do something like this:
Template.myTemplate.answers = function () { var self = this; // assume that this.answers is a list of possible answers return _.map(this.answers, function (answer) { return _.extend(answer, { questionId: self._id, }); }); }
Then you are good to go, and in your template you can do things like:
<template name="myTemplate"> {{#each answers}} <button data-question="questionId">...</button> {{/each}} </template>
BTW: Please note that the use of the attribute question in the element html incorrect in accordance with the standard. You should always attribute your own data- tags.
Also note that if you attach events to your templates as follows:
Template.myTemplate.events({ 'click button': function (event, template) {
then in the event callback you have access to this , which represents the context in which the button element was displayed, as well as template.data , which is the data context attached to your template instance, so this is actually more or less yours parent context. "
EDIT
Please note that the new template engine, i.e. blaze , allows us to use dot notation in templates, so the method described above is no longer needed, and {{../_id}} completely beautiful.
source share