What is the purpose of jQuery ($) in coffee script?

112 views Asked by At

I am new to jQuery and coffee script. During my journey I bumped several times into following construct at the beginning of the coffee script:

jQuery ($) ->
  $table = $('.container table')
  productListUrl = $table.data('list')
  ...

Which gets translated to following javascript

(function() {

  jQuery(function($) {
    var $table, productListUrl;
    $table = $('.container table');
    productListUrl = $table.data('list');
    return $.get(productListUrl, function(products) {
      return $.each(products, function(index, eanCode) {
        var row;
        row = $('<tr/>').append($('<td/>').text(eanCode));
        return $table.append(row);
      });
    });
  });

}).call(this);

I know that jQuery( ) and $() are just aliases but wondering if that has some additional meaning because I guess that ($)-> is equivalent to function($){ ...} and $ is a valid parameter name asfk in jQuery. I am using that in combination with play framework.

I am still missing some bits here. What is the wrapping function with applied call(this) for? I thought that this could be somehow related to ready( ) or the alternative syntax

$(function() {
...
});

Could someone put some shed on that?

1

There are 1 answers

0
user2595529 On

As you said, jQuery(function($) {...}) is a way to declare a callback for the DOM ready event.

The parameter $ of the callback function ensure that the $ symbol, inside this function, is the jQuery shortcut, as explained in http://api.jquery.com/jQuery/#jQuery-callback