Skip to content

Instantly share code, notes, and snippets.

@chamnap
Created March 14, 2013 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chamnap/5162352 to your computer and use it in GitHub Desktop.
Save chamnap/5162352 to your computer and use it in GitHub Desktop.
Manage javascript code in large rails app
NS.Loader =
exec: (controller, action="init") ->
ns = NS
controller = _.capitalize(_.singularize(controller))
ns[controller][action]() if controller? and ns[controller]? and ns[controller][action]?
return
init: () ->
## load underscore string
_.mixin(_.string.exports())
## find out controller and action
body = $(document.body)
controller = body.data("controller")
action = body.data("action")
## load by order
@exec("common") ## NS.Common.init() - application-wide code
@exec(controller) ## NS.User.init() - controller-wide code
@exec(controller, action) ## NS.User.show() - action-specific code
jQuery ->
NS.Loader.init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment