Dec 19, 2011

Don't Be A CoffeeScriptster


Brandon Satrom takes a thoughtful look at CoffeeScript in CoffeeScript is for Closers, a recent video recorded for the RedDirtJS Conference. He makes a number of great points, including:
CoffeeScript is not a replacement for learning JavaScript.
I couldn't agree more. However, I'd like to add my own rejoinder:
CoffeeScript is a wonderful inspiration for learning JavaScript. 
Programming CoffeeScript is so much fun, it makes diving deep into JavaScript all the more worthwhile.

Dec 14, 2011

Eco CoffeeScript Templates Presentation at URUG

The slides from my presentation to URUG on Eco (Embedded CoffeeScript templates) are now online: backbonecoffeescript.com/eco-coffeescript-templates.

The talk makes two major points:

  1. You can get started on the path to better-organized client code easily and incrementally by moving AJAX view rendering into embedded templates.
  2. The impact of Step 1 is easy to underestimate, but can be the basis for a tectonic shift in your application architecture, in which your server becomes a JSON-based web service.
Please comment here on the slides, I would love your feedback.

Dec 5, 2011

Little Book on CoffeeScript to be Published by O'Reilly

In September I reported that O'Reilly had a CoffeeScript book in the works. This evening Alex MacCaw confirmed that he just completed a new version of The Little Book on CoffeeScript, including a couple new chapters, to be published by O'Reilly in early 2012. I'm a big fan of the Little Book. Bravo!

Nov 27, 2011

CoffeeScript on CDN

Where is CoffeeScript's coffee-script.js available on a CDN?

Your Web pages can now load CoffeeScript from the CDNJS content delivery network (CDN). To find the latest version, visit CDNJS at this search query: http://www.cdnjs.com/#/search/coffee-script

Or, if you are not a visitor from the distant future, simply copy and paste the following code snippet into your page to load CoffeeScript version 1.1.2.


<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/coffee-script/1.1.2/coffee-script.min.js"></script>

In fact, this page loads coffee-script.js from the CDN. To confirm that the CDN is successfully serving the file, simple open your browser's JavaScript console and enter CoffeeScript.VERSION. You should see "1.1.2" in the console.

Nov 15, 2011

Why is Spine in CoffeeScript?

Witness this exchange in the GitHub issues for the Spine MVC framework, which creator Alex MacCaw migrated to CoffeeScript in August.

Why the move to CoffeeScript?

I was wondering why the move to CoffeeScript? Spine was a nice, zero-dependency alternative to Backbone with easy to understand Javascript… now I have to wade through CoffeeScript to see what’s going on under the hood as the compiled version is full of CoffeeScript magic. A real shame.

MacCaw’s reponse:

For the simple reason I believe CoffeeScript is better than JavaScript - I’ve even written a book on the subject: http://arcturo.github.com/library/coffeescript/ I’m never going to write another line of JS if I can help it. You should give CoffeeScript a chance.

And with that, MacCaw closed the issue.

Nov 9, 2011

backbone-on-rails vs backbone-rails vs rails-backbone

Confused? Me too. A new Backbone integration library for Ruby on Rails made its debut today on GitHub. So why do we now have three well-known GitHub projects for integrating Backbone with Rails? (There are even some minor ones out there, such as backbone-rails-rocks.) Let's look at each of the majors, in order of appearance.

codebrew/backbone-rails


codebrew/backbone-rails, which debuted in May 2011, is indisputably the most popular project, with over 500 watchers on GitHub. In addition to installing the necessary Backbone library files, it adds its own implementation of Backbone.sync with some helpful additions for Rails, such as a csrf-token header. It also immediately sets up a directory structure in app/assets/javascripts/backbone to hold your code. (I don't think backbone is the most helpful directory name, but more on that later.)

You can then use its scaffold generator to create a full-featured CoffeeScript implementation based on your model. Its form generation matches all model attributes to text inputs, unlike Rails' own scaffolding, which uses more appropriate input types when possible. But it does accept attributes, which is convenient.

Confusingly, the gem for codebrew/backbone-rails is named rails-backbone. This is not a typo in the Readme.

aflatter/backbone-rails


aflatter/backbone-rails also appeared in May 2011, apparently just after codebrew's project. It is minimalist, consisting of a simple installer that copies backbone.js and dependencies into your project, along with a little Sprockets manifest for the asset pipeline. At the time of this writing it still uses the latest release of Backbone. Don't discount it, because it may be exactly all that you need.

meleyal/backbone-on-rails


meleyal/backbone-on-rails arrived today, and already has over 100 GitHub watchers, as well as a mention on Hacker News. It distinguishes itself from its predecessors by following the directory and code organization conventions laid out in thoughtbot's Backbone.js on Rails ebook. (The book costs $49 for a single reader, but you can view the conventions in the sample chapter).

Here's what the install output looks like:

$ rails generate backbone:install
      insert  app/assets/javascripts/application.js
      create  app/assets/javascripts/collections
      create  app/assets/javascripts/models
      create  app/assets/javascripts/routers
      create  app/assets/javascripts/views
      create  app/assets/templates
      create  app/assets/javascripts/backbone_on_rails_sample.js.coffee

And this is what the scaffold output looks like:

$ rails generate backbone:scaffold dog
      create  app/assets/javascripts/models/dog.js.coffee
      create  app/assets/javascripts/collections/dogs.js.coffee
      create  app/assets/javascripts/routers/dogs_router.js.coffee
      create  app/assets/javascripts/views/dogs
      create  app/assets/javascripts/views/dogs/dogs_index.js.coffee
      create  app/assets/templates/dogs
      create  app/assets/templates/dogs/index.jst.eco

The scaffolded files are empty apart from their class declarations, but I imagine that will change in later releases. You may consider the blank state a good thing. Regarding directory structure, I think the important difference from codebrew/backbone-rails is that meleyal/backbone-on-rails makes the assumption that you will only be using one client-side MVC framework. A good assumption that leads to a simpler directory structure.


Oct 18, 2011

Eco Templates in Rails 3.1 Asset Pipeline

CoffeeScript is supported by default in the Rails 3.1 asset pipeline, so you would suppose that support for the Eco library for embedded CoffeeScript templates would be included as well. I did anyway, and was surprised when Rails 3.1.1 treated my new .eco file as a foreigner:

ActionView::Template::Error (no such file to load -- eco


How do I get Rails 3.1 to package my Eco templates?



Add the following line to your Gemfile:

gem 'eco'

Run bundle install and Sprockets (a.k.a. the asset pipeline) is now ready to compile .eco files in app/assets/javascripts to JavaScript.


How do I find and render my Eco templates in my CoffeeScript?



Let's assume you want your embedded template to live in:

app/assets/javascripts/todos/views

If you think you can name your template todo.eco or even todo.js.eco, you're wrong. (Todos is an archetypical JavaScript framework demo app.) Although the template compiles down to a JavaScript function, at the time of this writing, the JavaScript compiled from todo.js.eco will produce a runtime error. Apparently, a JavaScript templating system is required. The easiest solution is to use JST, which Sprockets provides by default. (jQuery Templates are another option, via the sprockets-jquery-tmpl gem.)

Thus,

app/assets/javascripts/todos/views/todo.jst.eco

can be rendered in your CoffeeScript using:

JST['todos/views/todo']()

For a full working example of Eco templates with Rails and Spine, check out Alex MacCaw's infinite scrolling screencast and source.

Update (12/14/2011): Slides from my presentation on Eco at URUG are now online: http://backbonecoffeescript.com/eco-coffeescript-templates

Oct 12, 2011

Backbone.js vs Spine at URUG

I had the opportunity to present last night at the Utah Ruby User Group (URUG, uv.rb section), about two lightweight JavaScript MVC frameworks that I'm very enthusiastic about: Backbone.js and Spine. For those who want to draw lines, the topic is a bit removed from Ruby, although I did serve my examples from Rails 3.1.1, providing a brief asset pipeline intro as well. And I showed and ran a sample of backbone-rails scaffolding. (The slides from the talk are anemic, as uv.rb is more of a roundtable/open format than a lecture.)

I have been using Backbone, in CoffeeScript, in my professional work, and I love it. Spine caught my eye shortly after I started using Backbone, and in my personal time I've been tinkering with it and with Spine Mobile. Both are by Alex MacCaw, who recently published a wonderful book for intermediate JavaScript developers, JavaScript Web Applications, which follows along with the development of Spine. To prepare for the talk, I spent the weekend reading the book and checking out Spine's current CoffeeScript source, as well as adapting some standalone demo apps to use Rails as a backend.

In the end I had four separate Rails apps in which I had scaffolded exactly the same model, Todo, to provide a backend for different versions of the classic Todos sample app. This led to a great moment during the talk in which after clicking around quite a bit in the client, adding and deleting items, I refreshed the page. To my surprise a completely different app loaded in the browser, the one based on backbone-rails scaffolding. After preloading the page for the talk, I had shut down the original app and started the other. This was proof of how well decoupled the front-end app can be: As long as the server provides the correct RESTful json api (and security features notwithstanding), any server will do! After the initial page load, the server is really decoupled from the presentation layer.

After my presentation, another member of the group followed with a demo of testing a real-life Backbone app, with Jasmine specs that were written in CoffeeScript. It was strong evidence of the positive difference CoffeeScript can make in readability.

Sep 30, 2011

CoffeeScript is Language #14 on GitHub by Popularity

CoffeeScript recently became the 14th most popular programming language, ahead of #15 ActionScript, as ranked by popularity on GitHub.

Although the significance of the GitHub ranking is debatable, as it is based apparently on the number of GitHub repositories in which a language appears, rather than by those in which it is the primary language, thus giving an unfair advantage to JavaScript (and indeed, similarly, to CoffeeScript). This is nonetheless a significant metric showing the rapid adoption of CoffeeScript.

Sep 13, 2011

Yehuda Katz Proposes Interactive Proposals

Yehuda Katz has written a great article encouraging the contributors to ECMAScript to abandon their academic process for new language features in favor of something more concrete and accessible to the "lay developer" (nice term).

Interestingly, with a nod to the Try CoffeeScript online app, he recommends that proposals be made more interactive:

By writing such a source-to-source translator, existing language users can experiment with the new syntax easily in a browser environment without requiring a separate compilation pass. This also allows users to build an in-browser translation UI (similar to try CoffeeScript), which can improve general understanding of the new syntax and produce important feedback.

I feel this could be one of those watersheds, in which an outdated form inherited from an earlier era is replaced with a new hybrid of text and interactive software. This is a fascinating glimpse of the future. Can you imagine the day when every proposal for a new public works project or real estate development is required to be accompanied by interactive online tools for exploring potential consequences and outcomes?

Sep 12, 2011

O'Reilly CoffeeScript Book

An O'Reilly book on CoffeeScript by Alex MacCaw (@maccman) appears to be in the works, judging by this recent tweet by MacCaw. MacCaw is the author of the some great open-source JavaScript and CoffeeScript projects, as well as the good but short The Little Book on CoffeeScript, which is freely available online.

I replied that I would like to see a loooong book on CoffeeScript. Maybe something along the lines of Hal Fulton's The Ruby Way?

Sep 7, 2011

Pragmatic CoffeeScript Book Tops List

Trevor Burnham's CoffeeScript book has topped the bestseller list at The Pragmatic Bookshelf once again:


Congratulations to both Trevor and CoffeeScript. This is pretty amazing if you ask me.

Aug 28, 2011

Node.js Knockout 2011: CoffeeScript

I tried to investigate how many of the approximately 300 teams in the Node.js Knockout this weekend are using CoffeeScript: There is a team named "CoffeeScript is for Closures" but otherwise, I couldn't discover much yet. It should be possible to get a count by looking at the teams' repos after the event; if so, I will do a follow-up post listing the teams that competed with CoffeeScript, hopefully with some analysis of their performance.

Significantly, the nodeknockout.com site itself is coded in CoffeeScript (source here on GitHub, CoffeeScript and other dependencies listed here). If you haven't visited yet, check it out now for a real-time display of the action on the #nodeknockout IRC channel.

Aug 25, 2011

Jeremy Ashkenas CoffeeScript Video GoRuCo 2011

The home page for CoffeeScript is a benchmark example of succinct, dense, and entertaining documentation. But if you haven't made it through it because you don't like reading, or if you have read it once but would like to see the movie version, you must check out this recent video of Jeremy Ashkenas at GoRuCo 2011. He's great, and so is the production quality of the video. I highly recommend.


GoRuCo 2011 - Jeremy Ashkenas - CoffeeScript for the Well-Rounded Rubyist from Gotham Ruby Conference on Vimeo.

Notice that like many great software designers, he uses his hands to describe things in terms of 3-dimensional space a fair bit.

Aug 22, 2011

Interview with Jeremy Ashkenas

Traveling back in time to this January 2011 interview with Jeremy Ashkenas, it's amazing to hear how much he played down the attractiveness of CoffeeScript for programmers:

If the question is "why is CoffeeScript not a DocumentCloud project?" - it's because I can't justify using it for the main DocumentCloud development. Imagine trying to hire someone. "You'll have to learn to use a new language that we made up..."
What a difference a few months can make. In April, about two weeks after the news broke on GitHub that CoffeeScript would be a default in Rails 3.1,  I was in a job interview where I was told that as of the Rails 3.1 release (perceived at the time to be just around the corner), all JavaScript coding would be done in CoffeeScript, and as much of the existing JavaScript codebase as possible would be ported. Well, I enthusiastically accepted what I hoped to be my first professional opportunity with CoffeeScript, and it has turned out to be fantastic. Rails 3.1 has still not been released, but that didn't stop us, we switched new development completely over to CoffeeScript in early summer.