Clouds in my CoffeeScript

Tuesday, January 7, 2014

The "mystery language" CoffeeScript has been around for about 5 years now and I only recently started to play around in it. Like TypeScript, I must add that it's a bit of a learning curve, but once you get the hang of it, it's smooth sailing from there. I've promised myself to keep this short and sweet, so I decided to write an article about calling a Windows Azure Mobile Service (WAMS) from CoffeeScript.
Let's face it, there aren't any smoke and mirrors here. CoffeeScript transcompiles down to JavaScript, like TypeScript does, so calling any REST-based service with the help of your favourite JS framework (in my case JQuery) shouldn't be any different.
In my previous post, I've created a Mobile Service that periodically polls tweets from Twitter and pushes it to a table called 'tweets'. So next, using the power of CoffeeScript, let's build a small sample that reads the tweets from the table and show it on the screen.
In this example, my weapons of choice are:
Here we have simple HTML with references to JQuery, WAMS JS client and a custom script, which is the product of compiled CoffeeScript:

The nice thing about Web Essentials is that it supports CoffeeScript out of the box, I’ve just added a file with a ‘.coffee’ extension and we’re good to go. This extension will generate the optimized JavaScript (including a minified version) for us upon save.
The app.coffee script:

When we save this, it immediately generates an app.js and app.min.js that we can reference from within the HTML page.
image
Comparing the generated JavaScript to the original CoffeeScript we notice the substantial difference in row count:
image
Till next time!
@FanieReynders

No comments:

Post a Comment

Community

Popular Posts

Archives

Contributors