Skip to content

Access bodyTemplate's data context from footerTemplate #26

Description

@Achtel

Hi,

I'm using the modal to display an image and have rendered an icon in the footer to allow the user to delete the image. But I cannot seem to access the _id variable that was given to the modal as a {{title}} context. How can I achieve this?

<!--
// Modal template
-->
<template name="modalImage">
  <img src={{imageURL}} class="modal"/>
</template>

<template name="modalFooter">
  <div class="modal-footer">
    {{#if ownImage}}
      <a class='delete-image'><i class='fa fa-trash-o fa-2x'></i></a>
    {{/if}}
    <button id="closeButton" class="btn-flat btn-custom">Close</button>
  </div>
</template>

//
// Materialize modal-title
//
$(document).ready(function($) {
  $(document).delegate('*[data-toggle="materialize"]', 'click', function(event) {
    event.preventDefault();

    // retrieve image variables
    var _id = $(this).data("title");
    var _src = $(this).attr("href");
    var _user = (Images.findOne(_id, {fields: {'userId': 1}})).userId;
    var _ownImage = (_user === Meteor.userId());

    MaterializeModal.display({
      bodyTemplate: "modalImage",
      footerTemplate: "modalFooter",
      title: _id,
      imageURL: _src,
      ownImage: _ownImage
    });
  });
});

Template.modalFooter.events({
  'click .delete-image': (event) => {
    event.preventDefault();

    MaterializeModal.confirm({
      title: "Confirm Delete",
      message: "Are you sure you want to delete this image?",
      callback: function(error, response) {
        if (response.submit) {
          console.log(_id);
          Images.remove({_id: _id}, function(error, result) {
               if(error) {
                 Bert.alert( error.reason, 'danger' );
               } else {
                 Bert.alert( "Image deleted", 'success' );
               }
               // end else
             });
             // end Images.remove
        }
      }
    });
  }
});

The console.log throws a Reference error stating that _id is not defined. Same issue if I try title...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions