Giter VIP home page Giter VIP logo

backbone-generators's Introduction

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().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.