Reputation: 1865
The same is true for 'get' methods too. Like get(this, 'agentName')
and this.get('agentName')
returns the same value.
In the official ember doc for get method, it shows that we are supposed to pass 2 values to get. Then how does this.get('agentName')
work accurately?
Upvotes: 0
Views: 895
Reputation: 18240
this.get(...)
is a shortcut for Ember.get(this,...)
. However it is only available on ember objects, so only Ember.get
works in plain js objects.
Have a look at this.get(...)
implementation.
Upvotes: 3
Reputation: 339
The getter and setter in Ember has been upgraded to handle unknown property
, computed property
and observers
. Not many people would use setUnknownProperty()
hook or the unknownProperty()
hook with get and set, but there are computed properties and observers throughout most people's code. More on computed properties and observers.
So, the basic difference between your set()
and this.set()
has to do with the context of your function. When you call just set(), it has to be defined in that scope or imported from somewhere to get things done. Howeve, with this.set() the scope here is this. Depending on where you are calling the function the scope changes. For instance if you are in a component, this
refers to the component
class itself. Similar would be the case with controllers, routes and other ember objects. If you have object of your own and it does not extend any of ember classes, then this
would still work with how it does in any other JavaScript code. So it will fall back to default getter and setter in JavaScript.
As you may have realized by now, when you call get(this, 'foo')
, you are calling JavaScript's getter function and pass it the current context with property to be searched for. And when you say this.get('foo')
you are calling get()
from Ember.Object
class, which can handle things I mentioned above. And as @Lux mentioned this.get('foo') is simplified to call Ember.get(this, 'foo').
Hope this helps. I do encourage reading Ember guides and API docs. Curent ember-cli and ember-data version is @2.12.0
Upvotes: 2