MyApp.president = Ember.Object.create fullName: (-> @get('firstName') + ' ' + @get('lastName') ).property('firstName', 'lastName')
How the hell am I suppose to read that?
MyApp.president = Ember.Object.create fullName: (-> @get('firstName') + ' ' + @get('lastName') ).property('firstName', 'lastName')
How the hell am I suppose to read that?
Someone said that CoffeeScript is designed to be more writable than readable, and I'd tend to agree with that. But frankly, after having worked with it for the better part of a year, I prefer reading it than JS for just about everything. It's an acquired taste, but don't knock it till you try it :)
If you write beautiful javascript, you will write beautiful coffeescript and it will be 20-60% smaller. Reading less lines is less reading, this helps me.
MyApp.president = Ember.Object.create(({fullName: (function() {return this.get('firstName')+' '+this.get('lastName');}).property('firstName', 'lastName')}));
MyApp.president = Ember.Object.create fullName: (-> @get('firstName') + ' ' + @get('lastName') ).property('firstName', 'lastName')
it's not exactly a shining example of good coffeescript, but I think this is equivalent js
MyApp.president = Ember.Object.create({
fullName: function(){
return this.get('firstName') + ' ' + this.get('lastName');
}.property('firstName', 'lastName')
})
I'd probably write it in cs as MyApp.president = Ember.Object.create
fullName: (
-> "#{@get 'firstName'} #{@get 'lastName'}"
).property('firstName', 'lastName')
(ie multi line rather than a single line) but that's very much just stylistic choice. It's also a lousy example of the benefits of cs as the cs version doesn't add any benefit over the plain js one.Anyway, style does matter and were this line written with readability in mind you wouldn't make this mistake.
Also, I would definitely abstract over @get and property:
makeProperty = (attrs...) ->
func = () -> (@get(x) for x in attrs).join(" ")
func.property(attrs...)
MyApp.president = Ember.Object.create(
fullName: makeProperty("firstName", "lastName")
)
The parens after create are not needed, the next indented line should tell us that this is a function with arguments, but I don't mind them here.