1

Update: My problem was caused by two functions returning and not just one. The callback function in the $.each loop was generating the returns in the if/else block. Even though I link it later in the question, there is a good question about how to override returns when CoffeeScript compiles to JavaScript. This is not a duplicate of that question; I had a more specific problem.

I'm trying to learn CoffeeScript for fun by porting some work I did in JavaScript with the jQuery Widget Factory. Assume $foos is a boolean array. I have a CoffeeScript function that looks similar to this:

$ = jQuery

activateCats = ($foos) ->   
   $.each $foos, (idx, foo) ->
      $bar = $(".cats").eq(idx)
      if foo
         $bar.addClass("ui-state-active")
            .removeClass "ui-state-disabled"
      else
         $bar.removeClass("ui-state-active")
            .addClass "ui-state-disabled"
      # add another empty return here
   return

The compiled JavaScript looks like this:

var $, doThings;

$ = jQuery;

activateCats = function($foos) {      // Function 1
   $.each($foos, function(idx, foo) { // Function 2
      var $bar;

      $bar = $(".cats").eq(idx);
      if (foo) {
         return $bar.addClass("ui-state-active").removeClass("ui-state-disabled");
      } else {
         return $bar.removeClass("ui-state-active").addClass("ui-state-disabled");
      }
   });
};

If I don't include the return at the bottom it compiles to this (note the return in front of my $.each loop):

var $, doThings;

$ = jQuery;

activateCats = function($foos) {
   return $.each($foos, function(idx, foo) {
      var $bar;

      $bar = $(".cats").eq(idx);
      if (foo) {
         return $bar.addClass("ui-state-active").removeClass("ui-state-disabled");
      } else {
         return $bar.removeClass("ui-state-active").addClass("ui-state-disabled");
      }
   });
};

I'm pretty sure I don't want those returns to be in my $.each loop. I'm not sure why CoffeeScript is adding them, because I have the empty return at the bottom of my function. I was having this problem before in other places but I read this thread and solved those issues. I've tried the various methods in that thread and none of them seem to work for this edge case.

How can I prevent CoffeeScript from adding these returns?

Community
  • 1
  • 1
abdkw
  • 153
  • 7
  • possible duplicate of [Is there any way to not return something using CoffeeScript?](http://stackoverflow.com/questions/7391493/is-there-any-way-to-not-return-something-using-coffeescript) – TheHippo May 16 '13 at 14:29
  • This question has be answered before. – TheHippo May 16 '13 at 14:29
  • @TheHippo If you read the last two sentences in this question you will see that I have tried those solutions but they didn't work. – abdkw May 16 '13 at 14:31

1 Answers1

0

As mentioned here:

activateCats = ($foos) ->   
   $.each $foos, (idx, foo) ->
      $bar = $(".cats").eq(idx)
      if foo
         $bar.addClass("ui-state-active")
            .removeClass "ui-state-disabled"
         return
      else
         $bar.removeClass("ui-state-active")
            .addClass "ui-state-disabled"
         return
   return

Will compile to:

var activateCats;
activateCats = function($foos) {
  $.each($foos, function(idx, foo) {
    var $bar;

    $bar = $(".cats").eq(idx);
    if (foo) {
      $bar.addClass("ui-state-active").removeClass("ui-state-disabled");
    } else {
      $bar.removeClass("ui-state-active").addClass("ui-state-disabled");
    }
  });
};
Community
  • 1
  • 1
TheHippo
  • 61,720
  • 15
  • 75
  • 100
  • It was not clear to me by the answers to that other question that you had to place an empty return everywhere CoffeScript attempts to compile a return. Does it apply returns frequently (in loops and logic blocks) so you can selectively turn them off with an empty return? – abdkw May 16 '13 at 14:45
  • No you can't. As the CoffeeScript docs quote: "Everything is an Expression (at least, as much as possible)". In most cases it does not do any harm. – TheHippo May 16 '13 at 14:55
  • Ok, I'm new to the syntax but I'm trying to figure it out. Thank you for answering my question rather than just writing it off as a duplicate. It was something simple, but it was an idiosyncrasy to me that appeared to be different from the other thread. – abdkw May 16 '13 at 15:05
  • 2
    @CaesiumFarmer I think you're misunderstanding what's going on. You could also solve this problem by putting a return after the `if/else` block. The reason that the `return` at the end isn't doing it is that you have 2 functions and the return you had was in the outer one. In CoffeeScript, each function will return the last expression in it. – Aaron Dufour May 16 '13 at 15:19