Skip to content

Instantly share code, notes, and snippets.

@joshcutler
Last active December 31, 2015 21:29
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 joshcutler/8047420 to your computer and use it in GitHub Desktop.
Save joshcutler/8047420 to your computer and use it in GitHub Desktop.
Event based JS on page load for rails App
if typeof(Object.create) != 'function'
Object.create = (o) ->
F = () -> { }
F.prototype = o
return new F()
fromPrototype = (prototype, object) ->
newObject = Object.create(prototype)
`for(prop in object)
{
if(object.hasOwnProperty(prop))
newObject[prop] = object[prop];
}`
return newObject
window.EventHandler = fromPrototype Array,
register_init: (names, fun) ->
if(typeof(names.forEach) == 'undefined')
n=names
names=[n]
for n in names
previously_registered=this[n]
this[n]=() ->
if(typeof(previously_registered) != 'undefined')
previously_registered.call()
fun.call()
init: (name) ->
if(typeof(this[name]) != 'undefined')
this[name]()
EventHandler.register_init ["home_show"], () ->
alert('herp a derp')
EventHandler.register_init ["user_show", "user_index"], () ->
alert('herp a lerp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment