- The definition of a method or a property in Ember is the same as in Vanilla JavaScript, but they are resolved different in Ember. You are not working with POJOs. Every time you extend from a Route/Controller/View, you are adding the Ember layer that processes methods and properties differently.
- No, is not always safe. In your case it is because it is overly simple.
- Yes, always access properties with
get
.
User interaction with your app is what causes changes in the properties of your objects, we handle those interactions with events. When an event in Ember is fired, it is not resolved immediately, instead it is put in a priority queue and resolved at a later time.
Properties are updated and read in an asynchronous form, if you access them directly using this
there is no guarantee that you'll get the most up to date value.
Check: Managing Asynchrony
When you make a change to a property in Ember, it does not immediately propagate that change. Instead, it invalidates any dependent properties immediately, but queues the actual change to happen later.
So, when you change the value of a property, this is roughly what happens:
- A property is changed
- All dependent properties are invalidated
- Queues the actual change for later
- Wait for any other event handlers, for the current user, to finish
- Flush the changes queue
- Until here, the property is actually updated (and
this
would work as usual)
- Until here, the property is actually updated (and
Imagine you use this
to read a property, but some other event occurs that changes its value. The new value won't be available until the queue is flushed, but this
reads immediately the property and returns the value of a property that is scheduled to be updated soon. You get stale data. The methods get
and set
manage this asynchrony for you and guarantee fresh values always.
When you have only one property, in the whole app, this asynchronous mechanism won't be noticed.
There are many different queues in Ember, and the fundamental mechanism behind all this is the run loop.