Skip to content

Instantly share code, notes, and snippets.

@marcioj
Created August 28, 2014 15:52
Show Gist options
  • Save marcioj/09ab22096cd7663a7c6b to your computer and use it in GitHub Desktop.
Save marcioj/09ab22096cd7663a7c6b to your computer and use it in GitHub Desktop.
Organize js files in rails
import Flash from "components/flash";
export default function() {
// called all the times
Flash.dismiss();
}
export edit function() {
// called in any edit action
}
export default {
allElements: function() {
return $("#flash_notice, #flash_alert");
},
dismiss: function() {
var self = this;
setTimeout(function() {
self.allElements().fadeOut("slow");
}, 3000)
}
}
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require turbolinks
//= require loader
//= require_tree .
$(function() {
var route = $('body').data("route");
var controller = route.controller;
var action = route.action;
callModule("application", action);
callModule(controller, action);
// Lookup a file with the name of the controller and call the exported functions named "default"
// and the current action name
//
// For instance, given a controller named users, and an action edit, the following is called:
//
// require("application")["default"]();
// require("application")["edit"]();
// require("users")["default"]();
// require("users")["edit"]();
function callModule(controller, action) {
if(!(controller in require._eak_seen)) { return }
var module = require(controller);
["default", action].forEach(function(method) {
if(typeof module[method] === "function") {
module[method]();
}
});
}
});
export default function() {
// called all the times when the users controller is used
}
export edit function() {
// called in any edit action of users controller
}
doctype 5
html lang="pt"
head
meta charset="utf-8"
meta name="keywords" content=""
meta name="description" content=""
title
= page_title(app_name: 'pirtub')
link href="/favicon.ico" rel=("shortcut icon")
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
/Use main.js as entry point
= javascript_include_tag 'main', 'data-turbolinks-track' => true
= csrf_meta_tags
/![if lt IE 9]
script src="http://html5shim.googlecode.com/svn/trunk/html5.js"
/Pass the current controller and action so we can get it in the js
body class=body_class data-route="#{ { controller: controller_name, action: action_name }.to_json }"
= render 'header'
= render 'flash_messages'
= yield
= render 'footer'
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( main.js ) # include main.js in the asset pipeline
# ...
gem 'jquery-turbolinks' # for not bother with vendor plugins not loading
gem 'es6_module_transpiler-rails' # to use es6 modules
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment