Should I lock `get ()` s in Ember, or can I use dot notation?

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 a reason not to use the second method?

+4
source share
2 answers

I don’t remember where I read it on the ember website, but they suggested that the point solution was the best solution.

this.get('controller.simpleSearch.selectedOptions.height')
+2
source

Looking for an answer, I found this topic: The Ultimate Guide to Using .get at discuss.emberjs.com .

According to gordon_kristan 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 not an Ember object,
Ember.get(obj, 'very.deep.nested.property');

get() - Ember . , , PromiseObject ( Ember-Data ):

// 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');

, christopher :

obj.get('very.deeply.nested.property') undefined , obj - undefined. - undefined, get() undefined. get() , , - undefined.

, ember-metal/lib/property_get.

+1

Source: https://habr.com/ru/post/1529113/


All Articles