title: Wrapping meteor.js handlebars templates in coffeescript classes
body: <p>I'm totally digging on meteor, but I'm stuck trying to cut down on the global-ness of the examples and add a dash of OOP.</p>
  
  <p>Currently, my code looks like this:</p>
  
  <pre><code># View for Search Form
  form = Template.SearchForm  
  form.events =
    'submit #search_form' : query_submitted
    'click #load_more' : -&gt; Songs.get_next_page()
    'focus #query' : clear_query_field
  
  form.page = -&gt; Songs.page
  form.total_pages = -&gt; Songs.total_pages 
  </code></pre>
  
  <p>But, a la spine or backbone, what I'd really like to have is something like this:</p>
  
  <pre><code>class SearchForm extends Template.SearchForm
    events:
      'submit #search_form' : query_submitted
      'click #load_more' : -&gt; Songs.get_next_page()
      'focus #query' : clear_query_field
  
  
    page : -&gt; Songs.page
    total_pages : -&gt; Songs.page
  
    # etc etc
  
  form = new SearchForm
  </code></pre>
  
  <p>What's the right way to wrap a handlebars template in meteor?</p>
  
  <p>I've managed to wrap Meteor.Collection, but because handlebars names the object after the template, I'm not sure the right way to do it for the Template.</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>@greg pointed out that you can use _.extend to add the properties.  That works, but what if I want to fold the event handler methods 'query_submitted' and 'clear_query_field' into the class?  Something like this:</p>
  
  <pre><code>_.extend Template.SearchForm,
    events :
      'submit #search_form' : @query_submitted
      'click #load_more' : -&gt; Songs.get_next_page()
      'focus #query' : @clear_query_field
  
    page : -&gt; Songs.page
    total_pages : -&gt; Songs.total_pages
  
    clear_query_field : (event) -&gt;
      console.log 'focus'
  
    query_submitted : (event) -&gt;
      event.preventDefault()
      Songs.clear()
      Songs.query = $('#query')[0].value 
      Songs.search()
  </code></pre>
  
  <p>Doesn't quite work.  The event handlers aren't being called properly and I get errors in the console like:</p>
  
  <blockquote>
    <p>Uncaught TypeError: Object [object Window] has no method
    'query_submitted'</p>
  </blockquote>
  
  <p>Similarly,</p>
  
  <pre><code>events :
      'submit #search_form' : (e) -&gt; @query_submitted(e)
  </code></pre>
  
  <p>Gives:</p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot call method 'call' of undefined</p>
  </blockquote>
  
  <p>So what's missing?</p>