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.pngimage - Removes default
public/index.htmlfile - Renames
README.rdoctoREADME.md
- chmod's log directory to 0777
- TODO: add db directory
- Sets default route to:
application#index
- Creates
app/views/applicationdirectory - Creates
app/views/application/index.html.erbfile
- Creates
app/assets/stylesheets/appsdirectory - Creates
app/assets/stylesheets/app/_base.css.scssfile - Renames
app/assets/stylesheets/application.csstoapplication.css.scss - Removes
require selfandrequire tree .fromapplication.css.scssfile - Imports
twitter/bootstrapandapps/_baseSCSS files
- Removes
jquery_ujsandrequire_tree .fromapp/assets/javascripts/application.jsfile
- 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:installInstallation 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:configby default
rails g marionette:configConfiguration 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 MODELNAMEThis will create a model, and ask to also create the corresponding collection along with the App.request handlers for returning the instances.