...
{{#each thing in things}}
{{thing.name}}
<button name="doThing">Do thing with thing id</button>
{{/each}}
...
template1.js
Template.template1.events({
'click button'(event, instance) {
// instance refers to the entire template here, not the context within the #each loop
}
});
If I want to attach events to each button do something that uses the thing id, how do I get that context? I know I can create another template and attach event handlers on that, but it’s a bit tedious to create new templates for each time I want to iterate
Thanks for responses. Not really a fan of either of these solutions (for the first I’m unhappy about losing the namespacing and the second I’m sometimes trying to encode an object which is a bit of a pain), but I’ll work with it.
Anyone have an update on this? Tacking on data to the DOM just to be able to retrieve it in an event handler is inconvenient and repetitive.
I did find out that if you place the #each body in a separate <template></template>, then the event handler’s this will have that template’s context data, but that could end up with a lot of templates.
@coagmano I actually ended up placing the #each body in a separate <template></template>, then the event handler’s this has that template’s context data. That works, but that could end up with a lot of unnecessary templates. Just seems like there should be a better solution than any of these workarounds!