1

I'm new to Coffeescript and I'm having trouble turning this Javascript:

Handlebars.registerHelper("debug", function(optionalValue) {
  console.log("Current Context");
  console.log("====================");
  console.log(this);

  if (optionalValue) {
    console.log("Value");
    console.log("====================");
    console.log(optionalValue);
  }
});

Into working Coffeescript. The part that I'm hung up on is, I think, how to pass the "debug" argument to the registerHelper function, and also pass in an anonymous function that takes an optional argument.

This syntax:

Handlebars.registerHelper: "debug", -> (optionalValue)
  console.log("Current Context")
  console.log("====================")
  console.log(this)

  if optionalValue
    console.log("Value")
    console.log("====================")

Is not working for me.

1 Answer 1

5

You have the param and the -> reversed.
You also don't need the semi-colon since you are calling the registerHelper function.

Handlebars.registerHelper "debug", (optionalValue) -> 
 console.log("Current Context")
 console.log("====================")
 console.log(this)

 if optionalValue
  console.log("Value")
  console.log("====================")
  console.log(optionalValue)

Which compiles out from coffeescript to javascript as:

Handlebars.registerHelper("debug", function(optionalValue) {
  console.log("Current Context");
  console.log("====================");
  console.log(this);
  if (optionalValue) {
    console.log("Value");
    console.log("====================");
    return console.log(optionalValue);
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much for the response - I appreciate you taking the time to reply. Works perfectly.

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.