Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Rails 3 generators for creating Backbone.js assets
Ruby
branch: master

This branch is 19 commits ahead of gregerolsson:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
.gitignore
Gemfile
README.markdown
Rakefile
backbone-generators.gemspec

README.markdown

What is it?

A set of Rails 3 generators that generate client-side artefacts for use in applications based on Backbone.js. The generators were initially used to encapsulate the design patterns that we use in an application of ours, but they may be useful in other projects as well.

What do they create?

For a quick overview of the source code generated by the generators, see X.

Currently, there are only two generators available of which the most important is a scaffold generator that creates a bunch of artefacts based on an existing ActiveRecord model. It takes the name of an existing ActiveRecord model as input (e.g. 'Milestone'):

rails generate backbone:scaffold Milestone
  • A Backbone.Model with corresponding support for validation, I18N, and associations.
  • A Backbone.Controller with RESTful routes for managing various views.
  • Backbone.Views for displaying collections and individual models.
  • Skeleton SCSS stylesheets for styling the rendered views.
  • Handlebars.js templates containing the markup for the views.

As you notice, the generated source code is at this point specific to Sass (and the SCSS variation in particular) and Handlebars.js. The intention is later to offer a few other options for styling and rendering and you are encouraged to share your template modifications while we set up a way to opt in for these.

We realize that the generated source code may not be suitable out-of-the-box for many applications, which is why we encourage you to tweak the templates to suit your particular application. We believe that for mid-sized to large applications this work pays off, especially if there are several developers in your team and you need to make sure they structure their code in the same way -- this project is as much about enforcing design rules and best practices as it is about preventing repetetive typing (although your definition of 'best' may greatly differ from ours :-).

A generator for mixins

Rather than repeating code that is common for all models, views, or controllers, we have included a set of mixins that can be applied to these. The mixins are added to your project using the backbone:mixins generator and include:

  • RESTful routes and actions for controllers
  • I18N with support for Rails translation files

Patterns used

As mentioned, the existing templates were designed with a particular project in mind to make sure a set of patterns decided for that project are used everywhere. To summarize, these are:

  • Backbone models expose accessor functions in addition to the built-in get('attribute') inherited from Backbone.Model. The reasoning behind this is to offer a consistent API whether you are reading a value stored in the ActiveRecord back-end or a calculated value.
  • has_many associations in the ActiveRecord model are converted to nested Backbone.Collections in the Backbone model, accessible via access functions. By default, only has_many associations marked with :dependent => :destroy are managed by nested collections. The reasoning behind this is that for us this was a clear indication of a composition where you would normally have the nested collection included in the JSON returned from the server.

For instance, if a User model has first_name and last_name attributes a helper function may be added to return the fullName(). Rather than accessing first and last names differently than with the full name, the first_name and last_name attributes are encapsulated in accessor methods firstName() and lastName().

Something went wrong with that request. Please try again.