title: How to work with callbacks in meteor?
body: <p>I've created a helper for displaying a generic modal in my Meteor app (in coffeescript).</p>
  
  <p>Here's modal.coffee:</p>
  
  <pre><code>showModal = (modalType, title, body, callback) -&gt;
    validModals = [ "Error", "YesNo" ]
  
    if not modalType in validModals
      alert "Invalid modal type specified"    # @todo - find a better way of handling this error
  
    Session.set "modalType", modalType
    Session.set "modalTitle", title or ""
    Session.set "modalBody", body or ""
    modalCallback = callback or undefined
    Session.set "showModal", true
  
  Template.modal.title = () -&gt;
    Session.get "modalTitle"
  
  Template.modal.body = () -&gt;
    Session.get "modalBody"
  
  Template.modal.response = () -&gt;
    switch Session.get "modalType" 
      when "Error"
        [{
          css: 'cancel',
          message: 'OK'
        }]
      when "YesNo"
        [
          {
            css: 'cancel',
            message: "No"
          },
          {
            css: 'btn-primary',
            message: "Yes"
          },
        ]
  
  Template.page.shouldShowModal = () -&gt;
    Session.get "showModal"
  
  Template.modal.events {
    'click .cancel': -&gt;  
      Session.set "showModal", false
      cb = modalCallback
      alert "here " + cb
      if cb
        cb(false)
    'click .btn-primary': -&gt;
      Session.set "showModal", false
      cb = Session.get "modalCallback"
      if cb
        cb(true)
  }
  </code></pre>
  
  <p>The template it quite boring.</p>
  
  <p>Here's my client code (as in the invoker of this helper):</p>
  
  <pre><code>Template.details.events {
    'click .remove': () -&gt;
      showModal "YesNo", 
        "Are you sure you want to delete this item?", 
        "Deleting an items can't be undone. Are you sure you want to delete?", 
        (response) =&gt;
          if response
            Items.remove this._id, (err) =&gt;
              if err
                showModal "Error", "Error removing item", err.reason
  
      return false;
  }
  </code></pre>
  
  <p>I can't get it execute the callback. All of the examples I've seen end up putting everything into the session, but apparently it can't convert the function to json so it doesn't deserialise it correctly when a user clicks on the ok or cancel buttons.</p>
  
  <p>How can I execute a callback when a user responds to my modal?</p>