Search code examples
javascriptjquerycoffeescript

Coffeescript equivalent to On Document Ready


I am trying to use Coffeescript, but I am having a problem on my web application. Its methods are not called until I reload the page.

I think that what is missing is the $(document).ready(function () { part, but I couldn't find how to do it in the web.

file_name.js (works great)

$(document).ready(function () {
  $(document).on('click', '.add_fields', function(event) {
    event.preventDefault();
    /* Act on the event */
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
  });
});

file_name.coffee (doesn't work)

jQuery ->
  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()

How can I fix this?


Solution

  • $(document).ready ->
    

    See example at https://github.com/jashkenas/coffeescript/blob/master/documentation/site/docs.coffee#L16