Monday, 20 June 2011

CoffeeScript Development in 3 mintutes

Any obvious reference to coffee needs a picture
The idea of CoffeeScript just sounds great me. I like the idea of compiling decent JavaScript from code you can actually read. But what do you do if you have no Node.js available to compile it?

Well the answer is to compile to JS at runtime. However, I had an awful time getting the right setup to get going. If you are interested in playing with CoffeeScript where Node.js is not available (I'm using it in work to show what it's capable of) then just follow these instructions.

Create a directory to work in and inside that create an index.html file thus:

<!DOCTYPE html>
<html>
<head>
    <title>CoffeeScript Demo</title>
    <script data-main="scripts/requirements" 
               src="scripts/require.js"></script>
</head>
<body>
    <h1>CoffeeScript Demo</h1>
</body>
</html>

and a directory called 'scripts' into which you put the following.

require([
    'scripts/jquery-1.6.1.min.js',
    'cs', 
    'cs!app',
]);

  • And a file called app.coffee containing:
square = (x) -> x * x
alert square 42

That's it!

Now load the index.html page into your browser and the CoffeeScript code app.coffee should execute.

Of course you will now have to go away and read the docs so that you know why that worked, but then so will I.

No comments:

Post a Comment