I'm trying to use CoffeeScript to write behaviors, but the top-level function wrapper that the coffeescript compiler does by default seems to prevent the modification of the Drupal.behaviors object.

Off the cuff, the only thing I came up with that worked was to precompile this with coffee -cb myfile.js:

`(function($) {
`
Drupal.behaviors.myStuff =
    attach: (context, settings) ->
       # Do stuff....
`})(jQuery);`

I'm sure it wouldn't be too hard to get the 'bare' option to be used by coffeescript-php, but the back-tick insert of raw javascript is ugly and seems to break the spirit of it.

Is there a reason not just just have this module also provide (function ($) {...})(jQuery); as an alternate top-level wrapper?

Comments

matt2000’s picture

Component: Code » Documentation
Assigned: Unassigned » matt2000
Status: Active » Needs review

Ok, I discovered a technique that avoids the back-ticks and doesn't require the 'bare' option:

myStuff = ($) ->
  Drupal.behaviors.myStuff = 
    attach: (context, settings) ->
       # code....

myStuff(jQuery)

Maybe this should be documented on the project page?

maedi’s picture

Is #1 similar to putting this at the top of the file?
$ = @jQuery

Why does calling Drupal from within an anonymous function let you access Drupal.behaviors? I would have thought this is the same wrapper bubble problem.

Could we try @Drupal.behaviors?

maedi’s picture

Status: Needs review » Fixed

So how'd you go? @Drupal.behaviors worked?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.