Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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