Javascript syntax and Ember setter/getters


#1

The more I write ember the more onerous the getter and setter syntax is for Ember objects and models. Especially around conditional statements. Just very verbose and tedious to write and read.

Wondering if anybody has some particularly clever shorthand code patterns or syntactic sugar. Short of using coffescript or some transpiler like emberscript.

In particularly this general pattern annoys me to no end.

if ( fooObject.get('barProp') ) {
  // do something with bar property
} 

Where as in ruby you could have a relatively expressive and tight one liner

doSomething unless fooObject.barProp.nil?

#2

I always thought I would get use to the syntax. After a year, I still hate find it annoying.

The good thing is that I got used to those nasty metamorph tags, data- attributes and IDs that pollute my beautiful markup. Even better is that does might actually go away one day with HTMLBars.

Edit: sounds like Ember 1.8 released recently removes the need for metamorph elements :smile:


#3

I might be alone on this, but I’ve never minded accessors. Accessing a property on a class directly is an OOP no-no, so the syntax doesn’t look that weird to me. As far as this:

I might store it in a temporary property like this:

var barProperty = this.get("barProperty");
if (barProperty){
    // Do something
}

I think many people would that verbose or wasteful, but readability and maintainability always trump terseness for me. That also gives you an easy testing hook if you have problems:

// var barProperty = this.get("barProperty");
var barProperty = "A hard coded value for testing purposes";
if (barProperty){
    // Do something
}

Or

var barProperty = this.get("barProperty");
console.log("Testing the value of barProperty", barProperty);
if (barProperty){
    // Do something
}

#4

@kylecoberly this is exactly what I end up doing. It does seem verbose but is far more readable even at the cost of having to write more code.

Kind of wish there was an more automated way to do this. I suspect I just need to setup a snippet or editor shortcut that expands to this pattern.


#5

I wonder if a sweet.js macro could help out here.


#6

@samselikoff Thanks for the link. That does look interesting. Kind of like coffeescript but customizable. Oh the naughty things we could do with all this syntactic sugar! :smile: