Skip to content

Instantly share code, notes, and snippets.

@wolflee
Forked from doitian/app.js.coffee
Created December 26, 2013 06:33
Show Gist options
  • Select an option

  • Save wolflee/8130486 to your computer and use it in GitHub Desktop.

Select an option

Save wolflee/8130486 to your computer and use it in GitHub Desktop.
class App
constructor: (@body) ->
# site global setup
start: ->
controllerName = @body.data('js_controller')
Controller = App[controllerName]
if Controller
@main = new Controller
@
window.App = App
$ ->
window.app = app = new App($('body'))
app.start()
<body data-js_controller="<%= js_controller %>">
...
</body>
# Default JS controller for all actions in current Rails controller
# Set a new value or get current value
def self.js_controller(new_value = nil)
@js_controller = new_value || @js_controller || self.name.sub(/Controller$/, '').gsub(/::/, /-/)
end
# Lets each action customize it
attr_writer :js_controller
# If action does not set a value, use controller default value
def js_controller
@js_controller || self.class.js_controller
end
helper_method :js_controller
class App.Welcome
constructor: ->
# setup
class WelcomeController < ApplicationController
js_controller 'Welcome'
def new
# use default 'Welcome' js controller
end
def create
# use a different controller
self.js_controller = 'WelcomeCreate'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment