Should I use:
this.get('controller').get('simpleSearch').get('selectedOptions').get('height')
or
this.get('controller.simpleSearch.selectedOptions.height')
I think the first is... verbose. Is there any reason not to use the second method?
Can not remember where I read it on the ember site but they suggested the best solution was the dot notation.
this.get('controller.simpleSearch.selectedOptions.height')
While in pursuit of an answer, I found this thread: Definitive guide of when to use .get on discuss.emberjs.com.
According to gordon_kristan's answer:
Always use get(), and use it in one of the following two ways:
// If obj is guaranteed to not be null or undefined obj.get('very.deep.nested.property'); // If obj might be null or undefined, or if it's not an Ember object, Ember.get(obj, 'very.deep.nested.property');
Using get() is the only way to ensure that the Ember computed properties will always function properly. For instance, in your example, consider if model was a PromiseObject (which Ember-Data uses quite a bit):
// This will not work, since it won't activate the `unknownProperty` handler on `model` var startDate = parentView.controller.model.createdAt; // But this will work var startDate = Ember.get(parentView, 'controller.model.createdAt');
In addition, as christopher points out:
Using
obj.get('very.deeply.nested.property')
will only throw an undefined error ifobj
isundefined
. If any other property in the chain isundefined
, then the call toget()
will simply returnundefined
. If instead you calledget()
at every level, then it would throw an error if any level wasundefined
.
If you want to read the source, check out ember-metal/lib/property_get.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With