Questions tagged [coffeescript]
A language that transpiles to JavaScript. Use this tag for questions about programming with the CoffeeScript language.
coffeescript
9,745
questions
955
votes
5
answers
147k
views
How does Trello access the user's clipboard?
When you hover over a card in Trello and press Ctrl+C, the URL of this card is copied to the clipboard. How do they do this?
As far as I can tell, there is no Flash movie involved. I've got ...
768
votes
37
answers
904k
views
Why doesn't adding CORS headers to an OPTIONS route allow browsers to access my API?
I am trying to support CORS in my Node.js application that uses the Express.js web framework. I have read a Google group discussion about how to handle this, and read a few articles about how CORS ...
742
votes
26
answers
482k
views
How to use executables from a package installed locally in node_modules?
How do I use a local version of a module in node.js. For example, in my app, I installed coffee-script:
npm install coffee-script
This installs it in ./node_modules and the coffee command is in ./...
410
votes
15
answers
257k
views
How to run Gulp tasks sequentially one after the other
in the snippet like this:
gulp.task "coffee", ->
gulp.src("src/server/**/*.coffee")
.pipe(coffee {bare: true}).on("error",gutil.log)
.pipe(gulp.dest "bin")
gulp.task "clean",-...
335
votes
8
answers
110k
views
Ternary operation in CoffeeScript
I need to set value to a that depends on a condition.
What is the shortest way to do this with CoffeeScript?
E.g. this is how I'd do it in JavaScript:
a = true ? 5 : 10 # => a = 5
a = false ? ...
319
votes
9
answers
125k
views
How do I define global variables in CoffeeScript?
On Coffeescript.org:
bawbag = (x, y) ->
z = (x * y)
bawbag(5, 10)
would compile to:
var bawbag;
bawbag = function(x, y) {
var z;
return (z = (x * y));
};
bawbag(5, 10);
compiling via ...
263
votes
10
answers
167k
views
Exec : display stdout "live"
I have this simple script :
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee', function(error, stdout, stderr) {
console.log(stdout);
});
where I simply execute a ...
238
votes
10
answers
153k
views
NULL-safe property access & conditional assignment in EcmaScript 6+
Is there an operator that would allow the following logic (on line 4) to be expressed more succinctly?
const query = succeed => (succeed? {value: 4} : undefined);
let value = 3;
for (let x of [...
209
votes
6
answers
69k
views
how to write setTimeout with params by Coffeescript
Please tell me how to write javascript below in coffeescript.
setTimeout(function(){
something(param);
}, 1000);
192
votes
4
answers
103k
views
How to iterate over the keys and values in an object in CoffeeScript?
I have an object (an "associate array" so to say - also known as a plain JavaScript object):
obj = {}
obj["Foo"] = "Bar"
obj["bar"] = "Foo"
I want to iterate over obj using CoffeeScript as follows:
...
191
votes
8
answers
67k
views
Can I use CoffeeScript instead of JS for node.js?
What are my restrictions if I want to code node.js and use CoffeeScript?
Can I do anything I'd be able to do in JS?
152
votes
5
answers
77k
views
How do I comment in CoffeeScript? "/* this */" doesn't work
In what ways can you comment in CoffeeScript?
The documentation say you can use three hash symbols to start and close a comment block:
###
Comments
go
here
###
I've found that I can sometimes ...
151
votes
5
answers
158k
views
Uploading base64 encoded Image to Amazon S3 via Node.js
Yesterday I did a deep night coding session and created a small node.js/JS (well actually CoffeeScript, but CoffeeScript is just JavaScript so lets say JS) app.
what's the goal:
client sends a ...
150
votes
24
answers
163k
views
Can I determine if a string is a MongoDB ObjectID?
I am doing MongoDB lookups by converting a string to BSON. Is there a way for me to determine if the string I have is a valid ObjectID for Mongo before doing the conversion?
Here is the coffeescript ...
144
votes
4
answers
34k
views
CoffeeScript, When to use fat arrow (=>) over arrow (->) and vice versa
When building a class in CoffeeScript, should all the instance method be defined using the => ("fat arrow") operator and all the static methods being defined using the -> operator?
135
votes
9
answers
129k
views
React input defaultValue doesn't update with state
I'm trying to create a simple form with react, but facing difficulty having the data properly bind to the defaultValue of the form.
The behavior I'm looking for is this:
When I open my page, the ...
118
votes
16
answers
246k
views
React onClick and preventDefault() link refresh/redirect?
I'm rendering a link with react:
render: ->
`<a className="upvotes" onClick={this.upvote}>upvote</a>`
Then, above I have the upvote function:
upvote: ->
// do stuff (ajax)
...
117
votes
5
answers
35k
views
Is there any way to not return something using CoffeeScript?
It seems like CoffeeScript automatically returns the last item in a scope. Can I avoid this functionality?
115
votes
13
answers
42k
views
CoffeeScript on Windows?
How can I try CoffeeScript on Windows?
The installation instructions are only for *nix: http://jashkenas.github.com/coffee-script/#installation
EDIT:
Since I asked this a while ago, many new ...
114
votes
4
answers
26k
views
What does "Splats" mean in the CoffeeScript tutorial?
Looking at this CoffeeScript tutorial : http://jashkenas.github.com/coffee-script/
I don't quite see what the Splats is for. What is this construction? Where does it come from (historically)
111
votes
3
answers
61k
views
Simplest way to check if key exists in object using CoffeeScript
In CoffeeScript, what is the simplest way to check if a key exists in an object?
107
votes
9
answers
27k
views
Defining an array of anonymous objects in CoffeeScript
How do I define an array of anonymous objects in CoffeeScript? Is this possible at all, using the YAML syntax?
I know that having an array of named objects is quite easy:
items:[
item1:
...
101
votes
3
answers
71k
views
In CoffeeScript how do you append a value to an Array?
What is the prescribed way to append a value to an Array in CoffeeScript? I've checked the PragProg CoffeeScript book but it only discusses creating, slicing and splicing, and iterating, but not ...
96
votes
16
answers
46k
views
How can I manage client-side JavaScript dependencies? [closed]
Although there are great solutions to manage dependencies on the server side, I could not find any that satisfies all my needs to have a coherent client side JavaScript dependency management workflow. ...
93
votes
7
answers
14k
views
Has anyone used Coffeescript for a production application? [closed]
Coffeescript looks pretty cool. Has anyone used it? What are its Pros & Cons?
92
votes
12
answers
47k
views
Environment detection: node.js or browser
I'm developping a JS-app that needs to work both on the client side and the server side (in Javascript on a browser and in Node.js), and I would like to be able to reuse the parts of the code that are ...
90
votes
11
answers
64k
views
Easiest way to check if string is null or empty
I've got this code that checks for the empty or null string. It's working in testing.
eitherStringEmpty= (email, password) ->
emailEmpty = not email? or email is ''
passwordEmpty = not ...
89
votes
8
answers
64k
views
CoffeeScript Undefined
In javascript to check if a variable was never created, we just do
if (typeof MyVariable !== "undefined"){ ... }
I was wonder how I do that in coffeescript?... I try something like
if (MyVariable?...
88
votes
10
answers
14k
views
Are there any languages that compile to Bash?
I both love and hate writing Bash. I love that it's so streamlined for operating on files and working with processes (I agree with this popular question that it's way better in this regard than Python,...
87
votes
8
answers
148k
views
How do I get the Back Button to work with an AngularJS ui-router state machine?
I have implemented an angularjs single page application using ui-router.
Originally I identified each state using a distinct url however this made for unfriendly, GUID packed urls.
So I have now ...
86
votes
8
answers
20k
views
Coffeescript --- How to create a self-initiating anonymous function?
How to write this in coffeescript?
f = (function(){
// something
})();
Thanks for any tips :)
86
votes
1
answer
29k
views
Static classes and methods in coffeescript
I want to write a static helper class in coffeescript. Is this possible?
class:
class Box2DUtility
constructor: () ->
drawWorld: (world, context) ->
using:
Box2DUtility.drawWorld(w,c);
84
votes
11
answers
30k
views
Private members in CoffeeScript?
Does somebody know how to make private, non-static members in CoffeeScript? Currently I'm doing this, which just uses a public variable starting with an underscore to clarify that it shouldn't be used ...
80
votes
7
answers
39k
views
Function declaration in CoffeeScript
I notice that in CoffeeScript, if I define a function using:
a = (c) -> c=1
I can only get the function expression:
var a;
a = function(c) {
return c = 1;
};
But, personally I often use ...
78
votes
7
answers
52k
views
Conditional operator in Coffeescript
I really like this:
var value = maxValue > minValue ? minValue : maxValue;
Is there something equally concise in Coffescript?
78
votes
2
answers
57k
views
coffeescript check if not in array
Here's something simple to check if user is in moderator. But I want to check if user is not in moderator.
if err && user in moderators
return
Intuitively it would be like this
if err &...
76
votes
2
answers
52k
views
for (var key in object) in CoffeeScript? [duplicate]
How can I use for (var key in object) in CoffeeScript? It compiles to...
for (_i = 0, _len = object.length; _i < _len; _i++) {
key = object[_i];
...but I just want to iterate though an object....
76
votes
5
answers
169k
views
Create an ISO date object in javascript
I have a mongo database set up.
creating a new date object in mongoDb create a date object in ISO format
eg: ISODate("2012-07-14T00:00:00Z")
I am using node.js to connect to mongo database and query ...
75
votes
7
answers
32k
views
Examples of CoffeeScript in NodeJS?
As a pet project, I am trying to get familiar with NodeJS and CoffeeScript, and am finding it hard to get the ball rolling.
I've found plenty of examples of single-file super-simple apps like in ...
73
votes
3
answers
60k
views
How can I convert a JavaScript for-loop to CoffeeScript?
for (i = 0; i < 10; i++) {
doStuff();
}
That's the JavaScript code that I Want to convert to CoffeeScript.
70
votes
6
answers
215k
views
How to use mongoose findOne
I have the below schema (apologies that it is in coffeescript)
Schema = mongoose.Schema
AuthS = new Schema
auth: {type: String, unique: true}
nick: String
time: Date
Auth = ...
68
votes
5
answers
11k
views
Can I write npm package in CoffeeScript?
I have used CoffeeScript for a while. Now I need to write a npm package, can I write it in CoffeeScript, or I should compile CoffeeScript into JavaScript?
68
votes
5
answers
24k
views
How do I properly format long compound if statements in Coffeescript
If I had a complex if statement that I did not want to overflow simply for aesthetic purposes, what would be the most kosher way to break it up since coffeescript will interpret returns as the body of ...
65
votes
4
answers
17k
views
Method chaining with function arguments
What's the best way to chain methods in CoffeeScript? For example, if I have this JavaScript how could I write it in CoffeeScript?
var req = $.get('foo.htm')
.success(function( response ){
// ...
63
votes
5
answers
59k
views
Checkbox not binding to scope in angularjs
I am trying to bind a checkbox to scope using ng-model. The checkbox's initial state corresponds to the scope model just fine, but when I check/uncheck the checkbox, the model does not change. Some ...
63
votes
5
answers
15k
views
A Backbone.js Collection of multiple Model subclasses
I have a REST Json API that returns a list "logbooks". There are many types of logbooks that implement different but similar behavior. The server side implementation of this on the Database layer is a ...
61
votes
1
answer
38k
views
Coffeescript unmatched outdent error
I'm getting the error SyntaxError: Unmatched OUTDENT on line 9 when I try to compile the following coffeescript code. I'm not sure what I'm doing wrong. the indentation seems to be right, and I have ...
60
votes
2
answers
56k
views
Switch case statement in coffee script
I have a few different buttons that are calling the same function and I would like to have them wrapped in a switch statement instead of using a bunch of else if conditions. Any help would be great!!!
...
59
votes
8
answers
63k
views
Clean way to remove element from javascript array (with jQuery, coffeescript)
There are many questions about this, not least:
jQuery version of array contains, a solution with the splice method and many more. However, they all seem complicated and annoying.
With the combined ...
58
votes
2
answers
18k
views
Does CoffeeScript allow JavaScript-style == equality semantics?
I love that CoffeeScript compiles == into the JavaScript === operator. But what if you want the original JS == semantics? Are they available? I've pored over the documentation and can't find anything ...