Getting rid of CoffeeScript’s closure wrapper

Quick and dirty solution: Use the console flag -b (bare). Warning: Kittens will die if you do that! Clean solution: Don’t do that. Usage: coffee [options] path/to/script.coffee -c, –compile compile to JavaScript and save as .js files -i, –interactive run an interactive CoffeeScript REPL -o, –output set the directory for compiled JavaScript -j, –join concatenate … Read more

How can I manage client-side JavaScript dependencies? [closed]

RequireJS does everything you need. My answer to this question may help you. Example: Client app project hierarchy: sampleapp |___ main.js |___ cs.js |___ require.js main.js is where you initialize your client application and configure RequireJS: require.config({ baseUrl: “/sampleapp”, paths: { jquery: “libs/jquery”, // Local underscore: “http://underscorejs.org/underscore-min.js”, // Remote backbone: “https://github.com/documentcloud/backbone/blob/master/backbone-min.js” // Remote on GitHub … Read more

When does the “fat arrow” (=>) bind to “this” instance

The fat arrow binds at 3 occasions when declaring a method when declaring a function within a method when declaring a function in global context 1. When declaring a method When the Coffeescript compiler encouters the following syntactical pattern within a class declaration class A somemethod: (paramlist) => This will yield the following code within … Read more

Determining whether one array contains the contents of another array in JavaScript/CoffeeScript

No set function does this, but you can simply do an ad-hoc array intersection and check the length. [8, 1, 10, 2, 3, 4, 5, 9].filter(function (elem) { return arr1.indexOf(elem) > -1; }).length == arr1.length A more efficient way to do this would be to use .every which will short circuit in falsy cases. arr1.every(elem … Read more

How to access instance variables in CoffeeScript engine inside a Slim template

What’s happening is that “#{@user_name}” is being interpreted as CoffeeScript, not as Ruby code that’s evaluated and injected into the CoffeeScript source. You’re asking, “How do I inject a Ruby variable into my CoffeeScript source?” The short answer is: Don’t do this. The Rails team made an intentional decision not to support embedded CoffeeScript in … Read more

Add a line of code to ALL functions

You could try something like this, dynamically modifying your functions: var obj = MyClass.prototype; for (var prop in obj) if (typeof obj[prop] == “function”) // maybe also prop != “on” and similar (function(name, old) { obj[prop] = function() { var res = old.apply(this, arguments); Event.fire(name); return res; }; })(prop, obj[prop]);