Last active
December 31, 2015 21:29
-
-
Save joshcutler/8047420 to your computer and use it in GitHub Desktop.
Event based JS on page load for rails App
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
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]() |
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
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