Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Use RequireJS.</strong></p> <p>In one file called "my-classes.coffee", define the namespace.</p> <pre><code>define [], -&gt; # You need this if you want it to work in multiple environments. # Otherwise just use `window` to work in the browser. root = exports ? this root.MyClasses = {} </code></pre> <p>You can define your class in another file called "aa.coffee".</p> <pre><code>define ['my-classes'], (MyClasses) -&gt; class MyClasses.Aa @someProp: true </code></pre> <p>Another file:</p> <pre><code>define ['my-classes'], (MyClasses) -&gt; class MyClasses.Bb @someProp2: false </code></pre> <p>Now when you require, it should export <code>MyClasses</code> which includes <code>MyClasses.Aa</code>.</p> <pre><code>require ['my-classes', 'aa'], (MyClasses, _) -&gt; console.log MyClasses.Aa.someProp </code></pre> <p>One issue with doing it this way is that you can't depend on just "my-classes" in the <code>require</code> statement. If you did that, <code>MyClasses.Aa</code> would be undefined. But you can't depend on just "aa" either, because "aa" doesn't export anything except by adding to MyClasses. In the above code snippet, <code>MyClasses.Bb</code> is undefined because I haven't explicitly depended on it. This is why many people either use one giant file or duplicate the boilerplate of re-exporting the namespace.</p> <p>If anyone knows how to fix this, please let me know.</p> <p>I, personally, find RequireJS to be complicated to use, and there are many different ways to set it up. One way I've used it with jasmine is by using a cake task to precompile my CoffeeScript down to JavaScript, and then have spec files like this.</p> <pre><code>requirejs = require('requirejs') # Set the baseURL to your compiled JS dir. requirejs.config { baseUrl: __dirname + '/../lib' } requirejs ['my-classes', 'aa'], (MyClasses, _) -&gt; describe "someProp", -&gt; it "should be true", -&gt; expect(MyClasses.Aa.someProp).toEqual true </code></pre> <p>This may not be the best way, but I was able to use it to run modules in the browser, on a Node server, and in jasmine-node tests. I've also seen some people use <a href="http://blog.rjzaworski.com/2012/06/jasmine-coffeescript-and-require-js/" rel="nofollow noreferrer">custom runners</a> to avoid the boilerplate in their spec files.</p> <p>If you'd rather not use RequireJS, you may find <a href="https://stackoverflow.com/questions/8730859/classes-within-coffeescript-namespace">this question</a> helpful. It works by using the <code>namespace</code> function defined on the <a href="https://github.com/jashkenas/coffee-script/wiki/FAQ" rel="nofollow noreferrer">CoffeeScript FAQs</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload