How can CoffeeScript be written in CoffeeScript?

This is nothing new. C compilers have been written in C. Python has been written in Python. It’s possible to use a compiler for Language X to compile a newer version of itself, with more features. It’s called bootstrapping. Bootstrapping a language Writing a compiler in its own language How do you write a compiler …

Read more

Rails: access controller instance variable in CoffeeScript or JavaScript asset file

a couple of ways I have done this in the past put the data in hidden fields, access the data in js/coffee # single value <%= hidden_field_tag “foo_name”, @foo.name, { :id => “foo-name” } %> $(‘#foo-name’).val(); # when the ‘value’ has multiple attributes <%= hidden_field_tag “foo”, @foo.id, { :id => “foo”, “data-first-name” => @foo.first_name, “data-last-name” …

Read more

CoffeeScript – Referencing DOM nodes in Angular expressions is disallowed

As the error states, Angular disallows accessing DOM nodes in expressions. CoffeeScript uses an implicit return if none is specified. This means that for example the scope.open function in your code will: return element.dialog(‘open’); Angular will detect this and throw the error. If you add an explicit return it should solve the issue: scope.open = …

Read more

Does the ‘@’ symbol have special meaning in Javascript, Coffeescript or Jquery?

@ is not a valid character for a javascript identifier. Identifiers may only contain $, _, digits and letters. In coffeescript, @ means this. CoffeeScript has a few nice features related to the this keyword. First, CoffeeScript uses the @ symbol as shorthand for this.. For example, @foo is equivalent to this.foo. Second, if you …

Read more

Equivalent Ruby .times in Coffeescript

console.log ‘hi’ for [1..3] To also handle 0 correctly: console.log ‘hi’ for [1..n] if n Or with prototype magic: Number::times = (fn) -> do fn for [1..@valueOf()] if @valueOf() return 3.times -> console.log ‘hi’ Note that the second method isn’t recommended because changing the Number prototype has global effects. Edit: Changed according to @BrianGenisio’s comment …

Read more