“Can’t find variable” error with Rails 3.1 and Coffeescript

By default, every CoffeeScript file is compiled down into a closure. You cannot interact with functions from a different file, unless you export them to a global variable. I’d recommend doing something like this: On top of every coffeescript file, add a line like window.Application ||= {} This will ensure that there’s a global named … Read more

Is there a way to send CoffeeScript to the client’s browser and have it compiled to JavaScript *there*?

Jeremy already has this one, but let me add some important details and caveats: At 39k gzipped (compare to jQuery at 29k), coffee-script.js is a big file; so unless you’re actually letting your users run their own CoffeeScript, you really shouldn’t use it in production. As mentioned in the documentation, each CoffeeScript snippet will be … Read more

Exec : display stdout “live”

Don’t use exec. Use spawn which is an EventEmmiter object. Then you can listen to stdout/stderr events (spawn.stdout.on(‘data’,callback..)) as they happen. From NodeJS documentation: var spawn = require(‘child_process’).spawn, ls = spawn(‘ls’, [‘-lh’, ‘/usr’]); ls.stdout.on(‘data’, function (data) { console.log(‘stdout: ‘ + data.toString()); }); ls.stderr.on(‘data’, function (data) { console.log(‘stderr: ‘ + data.toString()); }); ls.on(‘exit’, function (code) { … Read more

React onClick and preventDefault() link refresh/redirect?

React events are actually Synthetic Events, not Native Events. As it is written here: Event delegation: React doesn’t actually attach event handlers to the nodes themselves. When React starts up, it starts listening for all events at the top level using a single event listener. When a component is mounted or unmounted, the event handlers … Read more

Uploading base64 encoded Image to Amazon S3 via Node.js

For people who are still struggling with this issue. Here is the approach I used with native aws-sdk : var AWS = require(‘aws-sdk’); AWS.config.loadFromPath(‘./s3_config.json’); var s3Bucket = new AWS.S3( { params: {Bucket: ‘myBucket’} } ); Inside your router method (ContentType should be set to the content type of the image file): var buf = Buffer.from(req.body.imageBinary.replace(/^data:image\/\w+;base64,/, … Read more

How does CoffeeScript’s existential operator work?

The documentation says this about ?: CoffeeScript’s existential operator ? returns true unless a variable is null or undefined, which makes it analogous to Ruby’s nil? so of course this will say “No taco!”: taco = undefined if taco? console.log “fiesta!” else console.log “No taco!” Your taco is explicitly undefined so taco? is false. CoffeeScript … Read more

Closure Scope not captured? — Coffeescript

This loop: for item in data.contents itemBox = $ “<div/>”, class: “itembox” is somewhat deceptive if you’re not used to (Coffee|Java)Script scope. The scoping actually looks more like this: itemBox = undefined for item in data.contents itemBox = $ “<div/>”, class: “itembox” so there is only one itemBox variable and that same variable gets used … Read more

Automatically set arguments as instance properties in ES6

Felix Kling’s comment outlines the closest you’ll get to a tidy solution for this. It uses two ES6 features—Object.assign and the object literal property value shorthand. Here’s an example with tree and pot as the instance properties: class ChristmasTree { constructor(tree, pot, tinsel, topper) { Object.assign(this, { tree, pot }); this.decorate(tinsel, topper); } decorate(tinsel, topper) … Read more