Skip to content

Route @model argument value timing issue #18987

@HeroicEric

Description

@HeroicEric

We ran into a difference in behavior between @model and this.model that I'm not sure how to best describe but here is a twiddle that demonstrates the issue:

https://ember-twiddle.com/29dd9ad4820bdf987c92eb7acf1d7697?openFiles=routes.index%5C.js%2Ctemplates.components.person%5C.hbs&route=%2Fsome-other-route

It seems that the value of @model changes to undefined before all of the components in the template have been destroyed. This is different than the behavior seen when using this.model, where the value remains until after all of the components in the template have been destroyed.

For example, if we return a value from the model hook:

// app/routes/index.js
export default class IndexRoute extends Route {
  model() {
    return {
      firstName: 'Little',
      lastName: 'Sebastian'
    };
  }
}

And pass that value to a component in the route's template:

{{! app/templates/index.hbs }}
<Person @person={{@model}} />

And try to access the value in the component's willDestroy lifecycle hook:

// app/components/person.js
import Component from '@glimmer/component';

export default class PersonComponent extends Component {
  willDestroy() {
    console.log({ person: this.args.person });
    // => { person: undefined }
  }
}

Transitioning from the index route to another route causes the value of @model to become undefined by the time the willDestroy hook for the component runs.

This issue goes away of we instead pass the value to the component using this.model:

{{! app/templates/index.hbs }}
<Person @person={{this.model}} />

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