CoffeeScript

Component ID

1407416

Component name

CoffeeScript

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Downloads

658

Component created

Component changed

Component body

For now I'd recommend using a preprocessor such as Atom or CodeKit until CoffeeScript PHP is made current.

CoffeeScript is a simple and elegant version of JavaScript.

This module provides CoffeeScript integration with Drupal, allowing you to write CoffeeScript code, which is automatically converted into JavaScript for use by the browser. CoffeeScript can also be used in combination with jQuery and some examples are below.

Usage

Add CoffeeScripts to your Drupal site by using one of the following:
<?php print drupal_add_js('script.coffee') ?> in your theme's html.tpl.php
scripts[] = script.coffee in your theme's .info file

Installation

Requires the Libraries and Prepro APIs.

  1. Download the CoffeeScript PHP library from GitHub.
  2. Place the extracted folder in your sites/all/libraries folder
  3. Download and enable this module

Examples

In CoffeeScript, the function keyword is replaced by the -> symbol, and indentation is used instead of curly braces.

A Drupal 7 jQuery before and after:

(function ($) {
  Drupal.behaviors.myCode = {
    attach: function (context, settings) {
      $('.button', context).click(function () {
        $(this).next('ul').toggle('show');
      });
    }
  };
})(jQuery);
$ = @jQuery

@Drupal.behaviors.myCode =
  attach: (context, settings) ->
    $(".button", context).click ->
      $(this).next("ul").toggle "show"