title: Correctly stopping an autorun() function when template is destroyed
body: <p>I have a template that contains a chart, rendered using MorrisJS. The chart should update when the <code>currentData</code> session variable is changed, so I have made it a reactive data source with:</p>
  
  <pre><code>Template.chart.rendered = function() {
  
      var template = this;
  
      Deps.autorun(function(c) {
  
          // Stop if the template is removed from the dom
          // Q: Is this really right?
          if(template.__component__.dom.parentNode() === null) {
              c.stop();
              return;
          }
  
          var results = Session.get('currentData');
  
          // ... render a chart with `results` as the data
  
          Morris.Bar({element: template.$(".chart-container"), data: results, ...});
  
      });
  
  };
  </code></pre>
  
  <p>Notice how I do a fairly horrid check for when to stop the autorun above. This was necessary because without this, when I navigate away from the page using the template (I'm using <code>iron-router</code>) to another page and back, I get warnings in the log like "Can't select in removed DomRange". I'm pretty sure this is happening because the template instance is removed, but the autorun is still running.</p>
  
  <p>I feel like I'm doing something wrong here, though. Is there (a) a better place to put the autorun so that it doesn't have this problem or (b) a better way to stop the computation when the template instance is removed from the DOM?</p>
  
  <p>I tried to find a way to do it with <code>created</code> and <code>destroyed</code> handlers, but I couldn't figure out how.</p>