1

I am learning Coffeescript and trying to convert the following JavaScript code to Coffescript and (I think) I am almost there. Mainly, I am having an issue with making the timer and popover_parent variables global:

$('.some_element').popover(
  {
    delay: { show: 350, hide: 100 }       
  }
);
var timer,
    popover_parent;
function hidePopover(elem) {
    $(elem).popover('hide');
}
$('.some_element').hover(
    function() {
      var self = this;
      clearTimeout(timer);
      $('.popover').hide(); //Hide any open popovers on other elements.
      popover_parent = self
      $(self).popover('show');            
    }, 
    function() {
      var self = this;
      timer = setTimeout(function(){hidePopover(self)},300);                 
});
$('.popover').on({
  mouseover: function() {
    clearTimeout(timer);
  },
  mouseleave: function() {
    var self = this;
    timer = setTimeout(function(){hidePopover(popover_parent)},300); 
  }
});

This is what I have so far:

$ ->
  $('.some_element')
    .popover ->
      delay: {show:350, hide: 100}
$ ->
  hidePopover = (elem) ->
    $(elem).popover('hide')

  $('.some_element').hover(
    ->
      self = $(this)
      clearTimeout(timer)
      $('.popover').hide()
      popover_parent = self
      $(self).popover('show')
    ->
      self = $(this)
      timer = setTimeout(
        ->
          hidePopover(self) 300
      )
  )

$ ->
  $(document).on({
    mouseover: ->
      clearTimeout(timer)
    mouseleave: ->
      timer = setTimeout(
        ->
          hidePopover(popover_parent) 300
      )
  })

Any suggestions would be greatly appreciated.

Thanks!

4
  • 2
    I suggest not using CoffeeScript at all. It's not making your code noticeably shorter or more readable. Commented Jan 8, 2013 at 19:13
  • 2
    this might help Commented Jan 8, 2013 at 19:20
  • 2
    From Blazemonger's comment, I should point out that there are two distinct camps on JS/CS: people that think CS dilutes the JS community with a "designer" language and people that think CS allows code to be more concise and readable. If you google "coffeescript vs javascript" you will see this internet-wide flame war in all its glory. Commented Jan 8, 2013 at 20:00
  • Converting javascript to coffeescript is like converting c++ to java. Will lead to ugly code imho. Commented Jan 8, 2013 at 22:13

1 Answer 1

2

I'm not sure if you knew about http://js2coffee.org, but if it works with your code it's a really quick way to learn how to convert Javascript to Coffeescript. I used it a lot when getting started with coffee.

It also does compilation of coffee to js, of course.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.