Accessing parent context from template's events

Hi all,

This is my first week with meteor and I am really enjoying the project so far. However, I am not sure how to accomplish the following:

I have a simple blog with posts in it. I have a collection of posts and each post has a tag array.
In my blaze template I have nested loops. The outer loop would print the posts and the inner loop would print the tags. I want to have a delete button where I can remove tags from the post.

In order for me to update the post, I need to have the post _id property. However, I was not able to access it.

I have made a meteorpad example:
http://meteorpad.com/pad/wFrnNJZiXKJku4vie/Blog%20with%20Tags
Please check line 12 from the client/app.js
I tried Template.parentData() but with no look.

Any idea?

Try the following

<template name="postTemplate">
  {{#each posts}}
      <h2>{{topic}} (ID = {{_id}})</h2>
      <p>{{content}}</p>
      <ul>
      {{#each tags}}
        <li>{{text}}<button post="{{../_id}}" text="{{text}}" class="delete">&times;</button></li>
      {{/each}}
      </ul>
{{/each}}
</template>

then you can do this in the event handler

Template.postTemplate.events({
  'click .delete': function(e,t){
    var $b = t.$(e.target);
    var text = $b.attr('text');
    var post = $b.attr('post');
    alert("deleteing Tag:" + text + " from post ID: " + post);
  }
});
1 Like

Ahh, someone beat me to it. But here’s my solution:

http://meteorpad.com/pad/DxSRbn6mbHfmhFatR/Blog%20with%20Tags%20V2

Thank you @jamgold and @ffxsam for helping. All of your solutions worked with me correctly, I decided to go with @ffxsam as his solution is more straight forward and does not modify the DOM :blush: .

1 Like

Meteorpad is dead. Do you remember what the solution was? Blaze sucks