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.


2 comments:

  1. The reason the name is _backwards_ in CODEBREW/BACKBONE-RAILS is this:

    https://github.com/codebrew/backbone-rails/commit/5303bda24ecbfd7c73efb0ec791773f2ceca9369

    # fixing gem require because backbone-rails gem is taken

    ReplyDelete
  2. Would you mind mentioning backbone-on-rails which is the version Thoughtbot suggests to use in their e-book.

    ReplyDelete