Backbone-generators is a project mainly written in Ruby, it's free.
Rails 3 generators for creating Backbone.js assets
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.
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
Backbone.Model
with corresponding support for validation, I18N, and associations.Backbone.Controller
with RESTful routes for managing various views.Backbone.View
s for displaying collections and individual models.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 :-).
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:
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:
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.Collection
s 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()
.