Sam Mason
Sam Mason

Reputation: 1037

Passing data from one template using a click event on another template

I'm starting with meteor and wondering how someone would do something like this.

I have lots of tasks listed out each one representing a single item in a collection.

I also have a single form template that is rendered elsewhere on the screen, when someone clicks on the task I would like the form to populate so that people can edit the data fields.

How do I go about passing the tasks data, from a click event on the task template to the form which is in a completely separate template.

Upvotes: 0

Views: 185

Answers (1)

255kb - Mockoon
255kb - Mockoon

Reputation: 6974

You can use Sessions. In your list item template:

Template.yourItem.events({
  'click .something': function() {
    Session.set('selectedItem', this._id);
  }
});

In your form template create a helper to retrieve the list item data:

Template.yourForm.helpers({
  selectedItemData: function() {
    return ItemsCollection.findOne({_id: Session.get('selectedItem')});
  }
});

Then just use your helper to populate your form using with for example:

{{#with selectedItemData}}
  <form>
    ...
    <input type="text" value="{{yourfield}}">
    ...
  </form>
{{/with}}

Upvotes: 1

Related Questions