How to pass parameters to template event in meteor?

Ramesh Murugesan picture Ramesh Murugesan · Apr 2, 2015 · Viewed 7.4k times · Source

How to pass values to template events

Html

<template name="Header">
 <div class="testClass">Text1</div> // pass a = 1
 <div class="testClass">Text2</div> // pass a = 2
</template>

Javascript

Template.Header.events({
'click .testClass':function(event, template){
    console.log(a) //print a values
  }
});

Answer

saimeunt picture saimeunt · Apr 2, 2015

You need to set the appropriate data context, using child templates for example :

HTML

<template name="Header">
  {{> test text="Text1" a=1}}
  {{> test text="Text2" a=2}}
</template>

<template name="test">
  <div class="test">{{text}}</div>
</template>

JS

Template.test.events({
  "click .test": function(event, template){
    console.log(this.a);
  }
});