Inspired by rails generate
- Generates Backbone.js models and collections with BackboneFactory and Jasmine tests.
The gem adds Backbone: Setup | Namespace | Model | Collection
$ rails generate backbone_generator:collection Dashboard::Widget
app/assets/javascripts/dashboard
├── collections
│ └── widgets.coffee
└── models
└── widget.coffee
spec/javascripts/dashboard
├── collections
│ └── widgets_spec.coffee
├── factories
│ └── widget_factory.coffee
└── models
└── widget_spec.coffee
Installation
Add this line to your application's Gemfile:
gem 'rails-backbone-generator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rails-backbone-generator
Usage
The gem has three main generators (and one for setup):
backbone:setup | backbone:namespace | backbone:model | backbone:collection
$ rails g # for generate
BackboneGenerator:
backbone_generator:collection
backbone_generator:model
backbone_generator:namespace
backbone_generator:setup
backbone:namespace
Creates a namespaced directory structure
$ rails g backbone_generator:namespace dashboard
app/assets/javascripts/dashboard
│ ├── collections
│ ├── models
│ ├── routes
│ ├── templates
│ └── views
└── dashboard.coffee
spec/javascripts/dashboard/
├── collections
├── factories
├── models
└── views
backbone:collection
Creates a model and plural-collection
$ rails g backbone_generator:collection Dashboard::Widgets
app/assets/javascripts/dashboard
├── collections
│ └── widgets.coffee
└── models
└── widget.coffee
spec/javascripts/dashboard/
├── collections
│ └── widgets_spec.coffee
├── factories
│ └── widget_factory.coffee
└── models
└── widget_spec.coffee
backbone:model
Creates a model w/out a collection
$ rails g backbone_generator:model Dashboard::Widget
app/assets/javascript/dashboard
└── models
└── widget.coffee
spec/javascripts/dashboard
├── factories
│ └── widget_factory.coffee
└── models
└── widget_spec.coffee
backbone:setup
[ Optional ]: Prepares the app for Backbone/Jasmine development.
- Vendors and requires core frameworks
- Adds common plugins and Jasmine helpers
- Configures Jasmine and adds a Guard file. Run
bundle exec guard
to run tests.
Runtime Dependency
brew install qt --build-from-source
[ Details ]
- required to run Jasmine specs with guard-jasmine-headless-webkit
- other generators do not require this option
$ rails g backbone_generator:setup
app/assets/javascripts
├── application.js
└── shared
├── core_extentions
│ └── collections_extentions.coffee
├── helpers
└── utils
spec/javascripts/
├── fixtures
├── helpers
│ ├── association_helpers.coffee
│ ├── common_helpers.coffee
│ ├── fake_host.coffee
│ └── headless_webkit_helper.coffee
└── support
└── jasmine.yml
vendor/javascripts/
├── backbone
│ ├── backbone-1.1.0.js
│ ├── backbone-bind-to-1.1.0.coffee
│ ├── backbone-handlebars-1.0.0.coffee
│ ├── backbone-query-0.2.3.coffee
│ ├── backbone-relational-0.8.6.js
│ ├── handlebars-1.1.2.js
│ └── underscore-1.5.2.js
├── browser_compatibility
│ ├── json2.js
│ └── localstorage-polyfill.js
├── jquery
│ ├── jquery-1.10.2.js
│ ├── jquery-ui-1.10.3.js
│ ├── jquery.easing-1.3.js
│ └── jquery_ujs.js
└── testing
├── backbone-factory.js
├── jasmine-jquery-1.5.93.js
├── jasmine-sinon-0.2.1.js
├── mock-ajax-2.0.0.js
└── sinon-1.7.3.js
Factories
Similar to ruby's FactoryGirl, BackboneFactory adds factories to Jasmine.js.
The generator adds Factory.model_name({optional:override})
and Factory.collection_name({optional:override})
# spec/javascripts/dashboard/factories/widget_factory.coffee
BackboneFactory.define 'widget', Dashboard.Models.Widget, ->
reminder: 'Remember to update with attributes'
hint: 'attributes can be other factories'
External Docs
Backbone.js and it's foundation Underscore.js
- Backbone Relational: Associations for Backbone Models Bundle.get('books')
- Backbone Query: A query DSL inspired by MongoDB. Includes matchers, logical operators, sorting, and paging.
- Backbone BindTo: Cleans-up the pattern of adding and removing event listeners to Backbone.Views
- Backbone Handlebars: Extension for better integration with Handlebars.js including nested views
- Backbone Factory
- Jasmine Jquery
- Sinon: Spies, stubs, mock ajax, and time/animation
- Sinon-Jasmine: Test expectations for mocks and stubs
Best Practices: Backbone Patterns
Testing Backbone: Part 1: Introduction | Part 2: Models & Collections | Part 3: Routers & Views
Hackers Guide to Backbone: Part 1: Models & Events | Part 2: Collections | Part 3: Router and History | Part 4: Sync
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
TODO
add jasmine to config/routes.rb # mount JasmineRails::Engine => "/specs" unless Rails.env.production?
[![Rafter.com Logo](https://raw.github.com/chip-miller/rails-backbone-generator/images/images/rafter.png "Rafter.com")](http://http://www.rafter.com/) RailsBackboneGenerator is maintained by [Chip Miller](/chip-miller) and and funded by [Rafter](http://http://www.rafter.com/)