Skip to content

Instantly share code, notes, and snippets.

@tatsuosakurai
Last active August 29, 2015 14:00
Show Gist options
  • Save tatsuosakurai/11360450 to your computer and use it in GitHub Desktop.
Save tatsuosakurai/11360450 to your computer and use it in GitHub Desktop.
JavaScript Setting for Rails Controller
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-ui
// Loads all Bootstrap javascripts
//= require bootstrap
//= require debug
//= require base
//= require_directory .
//= require_tree ./admin
$(function () {
var $body = $("body");
var controller = $body.data("controller").replace(/\//, "_");
var action = $body.data("action");
var activeController = MyApplication[controller];
if (activeController !== undefined) {
if (activeController["common"] !== undefined) {
activeController["common"]();
}
if ($.isFunction(activeController[action])) {
activeController[action]();
}
}
});
<% unless Rails.env.production? %>
# console.log を p で使えるようにする
window.p = (obj) ->
if window.console
console.log(obj)
else
alert(obj) # IE は alert
return obj
<% end %>
class SampleUtil
showMessage: ->
console.log 'show message...'
# ファイルの読み込み順が関わるのであれば、
# application.js で適切な順番に↓を追加する。
# //= require sample_util
# (application.js は上から順に require で指定されたものが読まれます。)
# 呼び出すときは
SampleUtil.showMessage()
##################################
# もしくは
@MyApplication.showMessage = ->
console.log 'show message...'
# 呼び出すときは
MyApplication.showMessage()
class UsersController
common: ->
console.log("UsersController#common index でも show でも UsersController 内のアクションなら表示される")
index: ->
console.log("UsersController#index")
show: ->
console.log("UsersController#show")
this.MyApplication.users = new UsersController
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment