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.