Skip to content

Rendering issue with a component and initial render #151

@alankila

Description

@alankila

Hello,

for some reason I'm observing a bug where the first, initial render of a component defined like this:

    export default class MyComponent extends Ember.Component {
        foo = true;
        bar = false;
    }

etc. renders as if foo is set to true, and bar is set to false, even when the template binds the variables to different values, such as if I do this:

{{my-component foo=false bar=true}}

If something causes the component to re-render, e.g. I might have a computed property on it that gets updated, then it seems to get all the right values afterwards. I think the cause is that the defaults set by the bindings get destroyed by the object's TypeScript constructor, which overwrites those properties, and that breaks the bindings.

A more verbose class definition such as this:

    export default class MyComponent extends Ember.Component {
        foo?: boolean;
        bar?: boolean;

        constructor() {
            super();
            if (this.foo === undefined) {
                this.foo = true;
            }
            if (this.bar === undefined) {
                this.bar = false;
            }
        }
    }

shows no issue, because it's careful to avoid overwriting whatever is already in foo and bar. Of course, from an OO theoretical point of view what Ember is doing here is nonsense because the properties defined in this class can't possibly have any values before the object's constructor is ready with them. The constructor is supposed to have a private copy of the object first, and only after it returns, the object is accessible from elsewhere. In this case, I think that the object gets constructed with these property values already grandfathered in, which is unusual from OO point of view.

A consequence of doing this workaround is that every use of this.get("foo") must now deal with possibility that the value is undefined, even when it never is, really. This mostly concerns TS compiler options such as strictNullChecks and strictPropertyInitialization. Leaving out the ? in foo? makes TypeScript compiler complain that this.foo is used before it is initialized, so that's not good either.

Would it be possible to defer setting up Ember's bindings somehow that TS constructor gets run first, and then any bindings would get written by Ember to the object?

I am using ember 3.0.0 and ember-cli-typescript 1.1.5 with typescript 2.7.2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions