Cyzanfar
Cyzanfar

Reputation: 7136

Formatting Javascript into CoffeeScript

I am building a web app using Ruby on Rails. I would like to add some Javascript to a CoffeeScript file

var month = <%= @pin.duedate.month %>;
  var day = <%= @pin.duedate.day %>;

  var date = new Date(year, month, day);
  $("#due_date_timer").countdown({until: date});

At the moment my CoffeeScript file (pins.js.coffee) includes this:

$ ->
    $('#pins').imagesLoaded ->
        $('#pins').masonry
            itemSelector: '.box'
            isFitWidth: true

What is the proper way to include the Javascript into a CoffeeScript file?

Update Followed Amadan's direction In pins.js.coffee

$ ->
    $('#pins').imagesLoaded ->
        $('#pins').masonry
            itemSelector: '.box'
            isFitWidth: true

             year = <%= @pin.duedate.year %>
                      month = <%= @pin.duedate.month %>
                      day = <%= @pin.duedate.day %>

                      date = new Date(year, month, day)
                      $("#due_date_timer").countdown({until: date})

I get an error

ExecJS::ProgramError 
 unexpected =
             year = <%= @pin.duedate.year %>

Upvotes: 0

Views: 323

Answers (3)

Amadan
Amadan

Reputation: 198436

The CoffeeScript equivalent of your code is exactly the same, but without var and ;.

Upvotes: 2

gruntledcustomer
gruntledcustomer

Reputation: 253

You can embed vanilla javascript in your coffeescript by using backticks.

http://coffeescript.org/#embedded

Upvotes: 1

user2473975
user2473975

Reputation:

If you have existing javascript code can try following ...

http://js2coffee.org/

which will convert javascript code to coffee script.

Upvotes: 1

Related Questions