Coffeescript: Invoking call on function declaration - coffeescript

Computed properties on ember views have the form
myComputedProperty: function() {
return doSomething();
}.property()
However, when I write this in coffescript as
myComputedProperty: ->
doSomething()
.property()
I get an error like "Parse error on line 5: Unexpected '.'". Am I doing something wrong, or is this a quirk of the interpreter I'm using (Mindscape VS plugin)?

The grammar of the language doesn't support this. You have to add parenthesis around the function:
myComputedProperty: (->
doSomething()
).property()

You can add () around the function, or you can make the syntax more coffeescript friendly:
prop = (fn) -> fn.property()
myComputedProperty: prop ->
doSomething()

Related

what does ":" mean in this case in coffee script?

I am new to coffee script. When I am looking at this document https://atom.io/docs/api/v0.198.0/CommandRegistry#instance-add
I see a code segment like,
atom.commands.add 'atom-text-editor',
'user:insert-date': (event) ->
editor = #getModel()
editor.insertText(new Date().toLocaleString())
while the function signature looks,
::add(target, commandName, callback)
So in the code segment, what does : on the second line mean? My understanding is the 'user:insert-date' before : is commandName in the signature. The thing after : is "callback". So : is a argument separator like a ,? I don't find this introduced in the coffee script document http://coffeescript.org
That colon is just part of an object literal. The braces around object literals are optional in CoffeeScript when there's no ambiguity. If we add the optional braces, we get something that looks more like JavaScript:
atom.commands.add 'atom-text-editor', {
'user:insert-date': (event) ->
#...
}
So atom.commands.add is being called with two arguments. The first is the string 'atom-text-editor' and the second is an object with one key ('user:insert-date') whose value is an anonymous function that takes a single argument.
Appending mu is too short's answer (the user is absolutely correct that the second parameter commandNamecan be an object without the explicit braces {})
Atom's sourcecode:
https://github.com/atom/atom/blob/v0.198.0/src/command-registry.coffee#L81
add: (target, commandName, callback) ->
if typeof commandName is 'object'
commands = commandName
disposable = new CompositeDisposable
for commandName, callback of commands
disposable.add #add(target, commandName, callback)
return disposable

konacha - helper methods coffeescript

I'm trying to define a helper method in konacha in coffeescript, something like this
#expect_int_is_universal = (i) ->
expect(i).to.equal 42
describe '#test', ->
it 'checks if integer is 42', ->
#expect_int_is_universal(42)
Is it possible in konacha?
edit: error log here:
update: the fix is putting it inside a beforeEach block
beforeEach ->
#expect_int_is_universal = (i) ->
expect(i).to.equal 42
describe '#test', ->
it 'checks if integer is 42', ->
#expect_int_is_universal(42)
mu is too short hasn't successfully converted his comment as the answer, but i'll provide it here below:
# (AKA this) inside your it callback is not the same # as at the top level so you're defining expect_int_is_universal as a method on one object but trying to call it as a method on another object. Try without the #s. I don't know enough Konocha, Mocha, or Chai to say any more than that

Create coffeescript class

I have simple class in coffeescript (this class is located in file.js.coffee):
class ExampleClass
constructor: (arguments) ->
makeSTH: (page) ->
makeSTHElse: (data) =>
I have another coffee file. I included above file and I tried to create instance of ExampleClass this way:
/#= require file.js.coffee
class ExampleClass2
constructor: (arguments) ->
#ex = new ExampleClass(sth)
But I got something like this:
ReferenceError: ExampleClass is not defined
I don't know how to correctly reference to ExampleClass. Thanks for all answers and sorry for my English.
CoffeeScript will compile each of the source file as a separated compilation unit. Each of the compilation unit will be wrapped inside a block, so that the global namespace won't be polluted by mistake. So, ExampleClass actually get compiled to something like:
(function () {
var ExampleClass;
ExampleClass = function (args) {}
...
}).call(this);
You can see that ExampleClass can only be accessed from the same source. In order to access it from other source file, you need to bind it to window.
class window.ExampleClass
constructor: (args) ->
...
PS. you're not allowed to use arguments as formal parameter name in CoffeeScript, as it has special meaning in JavaScript.
And /#= require file.js.coffee is not valid in CoffeeScript, you need to remove the leading /. I think that's just a typo.

Bind with coffeescript

How can I call native bind method of function-object with coffeescript ? This is the example of what I am trying to achieve:
window.addEventListener("load",function(e){
this._filter(true);
}.bind(this);
)
Just add some parentheses around the function so that you can .bind the right thing:
window.addEventListener('load', ((e) ->
this._filter(true)
).bind(this))
That will use the native bind method instead of the usual var _this = this trickery that CoffeeScript's => uses.

Coffeescript setInterval in class

I started writing coffeescript last week, as I am programming a new Play20 site where coffeescript is the standard. I want to update a getData function in my class every 5 minutes, but the setInterval function does not bind to my class. Only the first time it calls getData, because the 'this' object is still reachable, as the setUpdateInterval() function is called from within the constructor.
But after the first call, the setInterval does not have any connection anymore with the Widget instance, and does not know what the this.getData() function is (and how to reach it).
Does someone know how to do it?
Here is my code:
class Widget
constructor: (#name) ->
this.setUpdateInterval()
getData: ->
console.log "get Data by Ajax"
setUpdateInterval: (widget) ->
setInterval( this.getData(), 3000000 )
Now here some Javascript magic is required. Reference
class Widget
constructor: (#name) ->
this.setUpdateInterval()
getData: ->
console.log "get Data by Ajax"
setUpdateInterval: (widget) ->
callback = #getData.bind(this)
setInterval( callback, 3000000 )
This will work in almost all browsers (guess which one not), so the
function will have to be bound differently. Some coffeescript magic:
callback = => #getData
The problem is that you are executing the function, instead of passing a reference to it.
Now, it sounds like you need to also keep the scope of the instance. do and => can help with that.
setUpdateInterval: (widget) ->
setInterval (do =>
#getData), 3000000
true
compiles to
Widget.prototype.setUpdateInterval = function(widget) {
var _this = this;
setInterval((function() {
return _this.getData;
})(), 3000000);
return true;
};
you will note the code executes a self invoking function, which return a function, that creates a closure around this, locking it into the scope of the callback (as _this)
Also note that you don't need to pass widget to the method (you aren't using it anywhere), and you would invoke this function in your constructor, to set up the interval. Whatever you do, you only want to call this method once. You could just put the contents of the function in your constructor.
Finally, since coffeescript returns the value of the last statement from all functions, I throw a true in there, but that might no be necessary.
This is handy in node as well. It's a varient of Tass's answer.
class Widget
constructor: (#options = {}) ->
#options.interval ?= 1000
#setInterval()
timer: ->
console.log 'do something'
setInterval: ->
cb = #timer.bind #
setInterval cb, #options.interval
w = new Widget()