title: Rails 3.2 + CoffeeScript + Namespacing + Separate Files = Confusion
body: <p>I have one companion script file for a Rails model, that uses code I've broken down into a hierarchy of over a dozen classes, for things like jQuery/Bootstrap UI code, factoring out similarities between different types of dialog, and so on. Let's say I'm working with <code>articles.js.coffee</code> as the "main page script" here.</p>
  
  <p>I can define Coffeescript classes, namespace them as something like <code>window.ourproject.OurUIDialog</code>, and save them in separate, per-class source files such as <code>app/assets/javascripts/OurUIDialog.js.coffee</code>. Restart the Rails server, and that class can be subclassed, e.g., <code>window.ourproject.PostInfoDialog extends window.ourproject.OurUIDialog</code>. As long as <code>PostInfoDialog</code> is in <code>articles.js.coffee</code> (where the instantiation of the <code>PostInfoDialog</code> is), all is well.</p>
  
  <p>But, if I move the subclass (<code>PostInfoDialog</code>) out into a separate file, e.g., <code>PostInfoDialog.js.coffee</code>, then attempting to do anything at all with it within the main articles script produces </p>
  
  <blockquote>
    <p><code>Uncaught TypeError: Cannot read property 'prototype' of undefined</code></p>
  </blockquote>
  
  <p>Again:</p>
  
  <ol>
  <li>This revolves around a Rails model's companion script file, here called <code>articles.js.coffee</code>;</li>
  <li><code>window.ourproject.OurUIDialog</code> gets picked up whether it's in its own file or in <code>articles.js.coffee</code></li>
  <li><code>window.ourproject.PostInfoDialog</code> (which extends <code>OurUIDialog</code>) can only be used if it's <em>not</em> in a separate file, <em>even though</em> viewing the generated HTML shows <code>PostInfoDialog</code> being included with all the other script files.</li>
  </ol>
  
  <p>I'm tearing my hair out trying to figure this out, and I didn't have much left to begin with. Any ideas?</p>