Using Blaze.remove

Hey everyone.

I’m using Blaze.renderWithData to show a modal window like this:

<template name="fullStatModal">
  <div tabindex="-1" role="dialog" id="fullStatModal" class="modal fade">
    ...
      <button class="btn btn-default" data-dismiss="modal">Close</button>
    ...
  </div>
</template>
Template.office.events
  "click .officeFullStat": (event, template) ->
    Blaze.renderWithData Template.fullStatModal, @_id, document.body
    return

So clicking “Close” hide modal but it is still in DOM and next time I click .officeFullStat one more #fullStatModal inserted to DOM.

According to docs I have to manually remove rendered view, but I have no idea how to do it. Now I just remove it with JQuery:

$("#fullStatModal").remove()

But I believe the instance of template with all subscriptions still in runtime.

I guess it should be somethink like this:

view = Blaze.renderWithData Template.fullStatModal, @_id, document.body
Blaze.remove(view)

So let’s say I want to remove view when “Close” button of rendered template clocked. How can I achieve this?

Oh. Looks like I found solution.

Is it ok if I just delete template from itself?

Template.fullStatModal.events
  "click #closeFullStat": (event, template) ->
    Blaze.remove template.view

From your HTML it looks like you’re using Bootstrap. Take a look at how the leading bootstrap modal package (peppelg:bootstrap-3-modal) is doing this:

1 Like

Yes, already found it myself, thank you anyway.