Skip to content

Instantly share code, notes, and snippets.

@gosukiwi

gosukiwi/garber-irish.coffee Secret

Last active Apr 9, 2019
Embed
What would you like to do?
# Garber-Irish DOM-ready Execution
# http://viget.com/inspire/extending-paul-irishs-comprehensive-dom-ready-execution
#
# Implementation using jQuery.
#
GarberIrish =
initialize: ->
controller = $('body').data('controller')
action = $('body').data('action')
return unless controller? and action?
parts = controller.split('/')
parts.push(action)
parts = (@camelCase(part) for part in parts)
try
klassName = "App.Controllers.#{parts.join('.')}"
klass = eval(klassName)
klass?.initialize()
# private
# transforms `foo_bar` into `FooBar`
camelCase: (name) ->
newName = $.camelCase(name.replace(/_/g, '-'))
"#{newName[0].toUpperCase()}#{newName.substr(1)}"
$(document).on 'turbolinks:load', -> GarberIrish.initialize()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment