After developing conventions for working with Backbone/Marionette, I wrote my own rails generators and application template. These remove a lot of unnecessary files, and sets up sensible defaults for working with Backbone/Marionette projects.
The marionette-template.rb
is an application template used when creating a new rails application. To use this application template, in the terminal type:
rails new APPNAME -m https://raw.github.com/brian-mann/rails-generators/master/marionette-template.rb
- Sets Ruby 1.9.3 in Gemfile
- Removes default
app/assets/images/rails.png
image - Removes default
public/index.html
file - Renames
README.rdoc
toREADME.md
- chmod's log directory to 0777
- TODO: add db directory
- Sets default route to:
application#index
- Creates
app/views/application
directory - Creates
app/views/application/index.html.erb
file
- Creates
app/assets/stylesheets/apps
directory - Creates
app/assets/stylesheets/app/_base.css.scss
file - Renames
app/assets/stylesheets/application.css
toapplication.css.scss
- Removes
require self
andrequire tree .
fromapplication.css.scss
file - Imports
twitter/bootstrap
andapps/_base
SCSS files
- Removes
jquery_ujs
andrequire_tree .
fromapp/assets/javascripts/application.js
file
- thin
- rabl
- oj
- gon
- js-routes
- eco
- compass-rails
- bootstrap-sass-rails
- Document additional template actions
To access these generators, copy the generators
folder into your lib
directory. Now run the install command below:
rails g marionette:install
Installation will do the following:
- Configure the client side application name
- Pull in the latest versions of Backbone / Underscore / Marionette
- Create the initial directory structure for apps / views / components / controllers / mixins / config
- Require the load order and directories via the Asset Pipeline
- Create the Marionette Application instance file:
app.js.coffee
- Invokes
rails g marionette:config
by default
rails g marionette:config
Configuration will do the follow:
- Override Marionette Renderer for JST Eco files
- Create Base Entities for Models and Collections
- Create a Base Controller for automating regions and cleanup
- Create Base Views for view / itemview / layout / collectionview / compositeview
config/initializers/marionette.rb
still store your answers for use when other generators run.
rails g marionette:module MODULENAME [controllers]
This will create a module_app file and initial directory with corresponding AppRoutes for the passed in controller actions. This will also automatically generate the controllers, views, and initial layout template. Use this command to scaffold out your initial app modules.
Example: rails g marionette:module Users list show
would create:
- /users
- users_app.js.coffee
- /list
- list_controller.js.coffee
- list_view.js.coffee
- /templates
- list_layout.jst.eco
- /show
- show_controller.js.coffee
- show_view.js.coffee
- /templates
- show_layout.jst.eco
rails g marionette:controller MODULENAME [controllers]
This creates the same controller/view/template combination as above without creating the initial module_app file. Use this to add controllers to an existing app module.
rails g marionette:entity MODELNAME
This will create a model, and ask to also create the corresponding collection along with the App.request handlers for returning the instances.