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 %>
-
1You have a whitespace problem, your indentation defines your block structure so you need to be consistent with it.mu is too short– mu is too short2014年08月12日 06:14:49 +00:00Commented Aug 12, 2014 at 6:14
3 Answers 3
The CoffeeScript equivalent of your code is exactly the same, but without var and ;.
1 Comment
<%= ... %> are neither JavaScript nor CoffeeScript; you need to pass them through ERb before you use them (and in CoffeeScript's case, before compiler gets to work on it). It is not trivial. I suggest including those in your HTML template as a separate script tag, defining a global variable you can use in your CoffeeScript.If you have existing javascript code can try following ...
which will convert javascript code to coffee script.
3 Comments
<%= @pin.duedate.month %> in itYou can embed vanilla javascript in your coffeescript by using backticks.
Comments
Explore related questions
See similar questions with these tags.