RailsBackboneGenerator 
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
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
backbone:collection
Creates a model and plural-collection
$ rails g backbone_generator:collection Dashboard::Widgets
app/assets/javascripts/dashboard
backbone:model
Creates a model w/out a collection
$ rails g backbone_generator:model Dashboard::Widget
app/assets/javascript/dashboard
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 guardto 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
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?
RailsBackboneGenerator is maintained by Chip Miller and and funded by Rafter