Skip to content

Instantly share code, notes, and snippets.

@doitian
Created December 4, 2012 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save doitian/4200680 to your computer and use it in GitHub Desktop.
Save doitian/4200680 to your computer and use it in GitHub Desktop.
JS Start Entry. Solution 3 Sample for http://ruby-china.org/topics/7145
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
@doitian
Copy link
Author

doitian commented Dec 4, 2012

Solution 3 sample for http://ruby-china.org/topics/7145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment