Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a CoffeeScript Class on document.ready

Tags:

i have

class Main
   test:->
      alert "yay!"

in coffeescript, and i want to run that inside my index.html

<script>
    $(function(){
        //and obv Main.test(); doesn't work
    });
</script>

there is a note of this on the website, it says it wouldn't work. But I couldn't find how to make it work. any ideas? i need to find out what coffeescript closure wrapper is.

or does coffeescript execute after document.ready anyway?

thx!

like image 742
Devrim Avatar asked Jan 12 '11 00:01

Devrim


1 Answers

To execute Coffeescript after document.ready you can use jQuery like this:

$ ->
  # Put your function code here
  init()

What that is doing is running jQuery(function () { callback... }) like the 3rd section at this link: http://api.jquery.com/jQuery/

Which basically says this:

jQuery( callback ) Returns: jQuery Description: Binds a function to be executed when the DOM has finished loading.

I declare all my classes, etc outside of document ready and then call an init function to get it running at the appropriate time.

I hope that helps!

like image 176
Brandon Avatar answered Oct 05 '22 17:10

Brandon