Is there something odd about getters and setters?


#1

Is there something odd about getters and setters?

This article:

seems to highlight a problem with getters and setters - is there something here that Ember newbies should be aware of?

Cheers,

Kevin


#2

TLDR: Not really.

Getters and setters in ember work like you’d expect. That said, when you call get/set (or any method) on an object you should understand what that object is and refer to it’s documentation if it doesn’t behave as expected.

In the article you referenced the poster was created an Ember.ObjectController and was surprised by how get/set worked. The whole point of using an ObjectController instead of a Controller is to have this special get/set behavior. This isn’t specific to Ember it’s how any ObjectProxy works. Here’s the first line of it’s API doc:

Ember.ObjectController is part of Ember’s Controller layer. It is intended to wrap a single object, proxying unhandled attempts to get and set to the underlying model object

I guess the lesson is don’t randomly use an ObjectController (or any other object) without a basic understanding of what it is for.


#3

I am almost certain there is an observer somewhere changing the value. Might not be even in the code this person is managing, perhaps he’s passing it into a component which is observing and setting the value.

I seldom use the observer pattern, this is a good example of why. This isn’t a problem with Ember, it’s a problem with the observer pattern in general.

Maybe a week later I ran into another problem. Some data was changing when I didn’t expect it to. I looked everywhere I could think of that might affect the data, but couldn’t find anything. Again, I spent the better part of a day trying to track down the source of this problem. After awhile I was getting desperate, so I started putting print statements all over the place. I discovered that the data was changing in one particular function. I examined it carefully but couldn’t find any hint of this data being impacted. Eventually I isolated the problem to the following snippet:

console.log(this.get(‘foo’)); this.set(‘bar’, …); console.log(this.get(‘foo’));

The first log line showed foo with a value of 25. The second log line showed foo with a value of 0. This is utter madness!