Trying to get a deeper understanding of coffeescript syntax as it applies to ember.
Can someone explain the bottom line with parentheses in the coffeescript syntax?
Some code examples:
When it comes to simple calls to the extend method I have seen different forms of the coffeescript in the wild.
For example this
App.PostRoute = Ember.Route.extend({
model: function() {
return App.Post.find();
}
});
I have seen translated into both of the following variants:
coffeescript:
App.PostRoute = Ember.Route.extend
model: ->
App.Post.find()
vs
coffeescript:
App.PostRoute = Ember.Route.extend(
model: ->
App.Post.find()
)
But when you want to get more complicated and add more labelled objects such as events to an ember object it seems that the parentheses are required.
javascript
App.PostRoute = Ember.Route.extend({
events: {
expand: function() {
this.controller.set('isExpanded', true);
},
contract: function() {
this.controller.set('isExpanded', false);
}
},
model: function() {
return App.Post.find();
}
});
coffeescript:
App.PostRoute = Ember.Route.extend(
events:
expand: ->
@controller.set "isExpanded", true
contract: ->
@controller.set "isExpanded", false
model: ->
App.Post.find()
)
Am I correct to understand that the following is not valid?
e.g. remove the wrapping parentheses.
coffeescript:
App.PostRoute = Ember.Route.extend
events:
expand: ->
@controller.set "isExpanded", true
contract: ->
@controller.set "isExpanded", false
model: ->
App.Post.find()
Maybe the coffeescript with the dropped parentheses is analogous to the “Seattle Style” of ruby methods. But it seems like the safest thing is to always use the parentheses.
Thoughts? Opinions?
By the way I am all for general rubyist mindset of removing “noise” from the code, but my brain sometimes hurts manually jumping between these syntaxes (coffee, javascript, ruby, python, php, etc). Or rather spending all day in the land of unicorns sometimes makes it hard to come back down into to the bare knuckle grittty world of javascript parentheses and semi colons.