Created
December 4, 2012 04:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body data-js_controller="<%= js_controller %>"> | |
... | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App.Welcome | |
constructor: -> | |
# setup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Solution 3 sample for http://ruby-china.org/topics/7145