You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
initializing object or array properties using init: do we still need this with native classes? #566
Note that we set headers in init() because default property values should not be arrays or objects.
I think this prohibition is about avoiding leaking state.
Wouldn't using native classes supersede this implementation quirk?
If so, what is the recommended way to set those properties?
Activity
ghost changed the title [-]initializing complex properties using init: do we still need this with native classes?[/-][+]initializing object or array properties using init: do we still need this with native classes?[/+]on Mar 6, 2019
The two ways to initialize properties in native classes is in constructors or field initializers (class A { propName = [1, 2, 3] }).
However, since field initializers are still in stage 3 I would keep the constructor recommendation.
P.S. with native classes that extend from EmberObject, you should use init and not a constructor.
We should use class fields for this case, and stop teaching folks to initialize in constructor/init unless it's some fairly complicated initialization work.
As for init vs constructor, the gist is that if your parent class does not use init (or other EmberObject APIs), you're safe using constructor. Based on a quick look at the code for adapters/serializers/models, and our conversation with @runspired, I think it should be safe to use constructor in examples, but we should definitely prefer class fields.
Activity
[-]initializing complex properties using init: do we still need this with native classes?[/-][+]initializing object or array properties using init: do we still need this with native classes?[/+]locks commentedon Mar 6, 2019
The two ways to initialize properties in native classes is in constructors or field initializers (
class A { propName = [1, 2, 3] }).However, since field initializers are still in stage 3 I would keep the
constructorrecommendation.P.S. with native classes that extend from
EmberObject, you should useinitand not a constructor.pzuraq commentedon Mar 6, 2019
We should use class fields for this case, and stop teaching folks to initialize in
constructor/initunless it's some fairly complicated initialization work.As for
initvsconstructor, the gist is that if your parent class does not useinit(or other EmberObject APIs), you're safe usingconstructor. Based on a quick look at the code for adapters/serializers/models, and our conversation with @runspired, I think it should be safe to useconstructorin examples, but we should definitely prefer class fields.use static class fields instead of init logic (#577)