Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Smaller JS Packages w/ Rails 3 Asset Pipeline

Smaller JS Packages w/ Rails 3 Asset Pipeline

From my talk at SD Ruby on 5/3/12. Slides provide an overview of the Garber-Irish "DOM-based Routing" method in Rails. Remainder of presentation was centered around code available here:
https://github.com/mleglise/rails-dom-routing-example

Avatar for mleglise

mleglise

May 04, 2012
Tweet

Other Decks in Programming

Transcript

  1. var RAILS = window.RAILS = {}; RAILS.controllers = {}; RAILS.currentPage

    = { controller: body.getAttribute( “data-controller” ), action: body.getAttribute( “data-action” ) }; app/assets/javascripts/rails_init.js
  2. window.RAILS.controllers.users = { init: function(){ // Do things on every

    Users#action }, show: function(){ // Do things on Users#show } }; app/assets/javascripts/rails/users_controller.js
  3. window.RAILS.controllers.common = { init: function(){ // Do things on EVERY

    page } }; app/assets/javascripts/rails/common_controller.js
  4. var UTIL = {}; UTIL.exec = function( controller, action ){

    var ns = window.RAILS.controllers, action = (action===undefined) ? "init":action; if( controller !== "" && ns[controller] && typeof ns[controller][action] == "function" ) { ns[controller][action](); } }; app/assets/javascripts/rails_start.js
  5. ...continued // Trigger all 3 actions for the current page.

    UTIL.init = function(){ UTIL.exec( "common" ); UTIL.exec( RAILS.currentPage.controller ); UTIL.exec( RAILS.currentPage.controller, RAILS.currentPage.action ); }; $(document).ready( UTIL.init ); app/assets/javascripts/rails_start.js