1

If I have a child control (or grandchild in this case) that looks like this:

<my-control region-id.two-way="location.regionId"></my-control>

When can I expect regionId to be available? The docs sound like bind and then attached happens so shouldn't regionId be set prior to attached happening?

I have added some logging to the bind, attached, and regionIdChanged events and looking at the console, I see

bind
attached
regionIdChange to null from undefined (default value in a dropdown)
regionIdChange to 1 from null (1 is the actual value)

I would have expected regionId to be 1 prior to attached happening. Is this the expected behavior?

Jesse
  • 3,522
  • 6
  • 25
  • 40
Joe
  • 5,389
  • 7
  • 41
  • 63
  • 2
    I think the problem in your parent viewmodel, where `location.regionId` is `null` at the moment when `bind` in child viewmodel executed. – Fabio Aug 17 '18 at 07:23
  • If the viewmodel is a component and the bindable field is propagated by the component's owning view in an asynchronous operation in its `bind` hook, it may happen, that `field` is "still" undefined in the component's `bind` hook. As far as I know the only hook where the completion is being awaited for, is the `activate` hook. – Schadensbegrenzer Aug 21 '18 at 08:35

1 Answers1

3

The variable should be filled in the bind() phase.

If you add logging to each of the phases:

export class Child {
  @bindable()
  public field: string;

  constructor() {
    console.info("constructor: ", this.field);
  }

  bind() {
    console.info("bind: ", this.field);
  }

  attached() {
    console.info("attached: ", this.field);
  }
}

It produces the following log output:

constructor:  undefined
bind:  test
attached:  test

Where test is the value that I bound it to.

Jesse
  • 3,522
  • 6
  • 25
  • 40
  • 1
    imo "the variable should be filled in the `bind()` phase" should rather be "the variable COULD be filled in the `bind()` phase". Again, if the owning viewmodel resolved the representation of `field` in a lengthy async operation in its `bind()` hook, it is very likely that `field` will be undefined in `Child`. If you want to ensure that the data resolved in a parent viewmodel is accessible in the `bind` hook of the children components, you need to resolve the data in `attached` hook of the parent view model. – Schadensbegrenzer Aug 27 '18 at 10:31