Skip to content

Instantly share code, notes, and snippets.

@shreyas-satish
Created March 7, 2012 13:16
Show Gist options
  • Save shreyas-satish/1993044 to your computer and use it in GitHub Desktop.
Save shreyas-satish/1993044 to your computer and use it in GitHub Desktop.
Better Organization of Assets in the Rails Asset Pipeline
<!DOCTYPE html>
<html>
<head>
<title>Citizenmatters</title>
<%= stylesheet_link_tag :application, params[:controller] %>
<%= javascript_include_tag :application, params[:controller] %>
<%= csrf_meta_tags %>
<%= yield :head %>
</head>
<body data-controller = "<%= controller_name %>" data-action = "<%= action_name %>">
</body>
</html>
//= require jquery
//= require jquery_ujs
//= require script_loader
window.MYAPP_SCRIPTS = {
common: {
init: function() {
// Any script common to all views using this manifest
}
}
}
$( document ).ready( SCRIPTLOADER.init );
window.MYAPP_SCRIPTS.events = {
init: function() {
// Script common to all actions in events
},
new: function() {
// Script only for the new action in events
},
edit: function() {
// Script only for the new action in events
}
}
MYAPP_SCRIPTLOADER = {
exec: function( options ) {
var controller = options.controller,
action = options.action || "init";
if ( controller !== "" && MYAPP_SCRIPTS[controller] && typeof MYAPP_SCRIPTS[controller][action] == "function" ) {
MYAPP_SCRIPTS[controller][action]();
}
},
init: function() {
var body = document.body,
controller = body.getAttribute("data-controller"),
action = body.getAttribute("data-action");
MYAPP_SCRIPTLOADER.exec({controller: "common" });
MYAPP_SCRIPTLOADER.exec({controller: controller});
MYAPP_SCRIPTLOADER.exec({controller: controller, action: action});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment