0

ANSWERED BELOW

I have a problem with an Aurelia bindingcontext being replaced by the parent component.

I have a first component which has a sourceItem property which is bound in its view:

${sourceItem.Name}

This shows the correct value "Parent name"..

This component has a router-view in which another component is created. I can see this child component being created without problem. The problem is that this child component also has a sourceItem property which is bound in its view in the same way.

${sourceItem.Name}

This SHOULD show the value "Child name" but although I can see this second sourceItem being correctly created, the view displays the value of the parent binding context : "Parent name"

If I change the name of one of the sourceItem (to sourceItem1 for instance), everything works fine. Any idea if I did some mistake or if there is a bug hanging somewhere?

Thanks a lot!

EDIT FOR MORE INFO I noticed that, when navigating to the child route, the problem occurs to me. But when I entered the full URL in the browser's bar and load the page, the binding works correctly. When I check the bindingContext & overrideContext (in the bind() method), the results are exactly the same. So it looks like it could be a timing issue...

Gaet
  • 699
  • 3
  • 11

2 Answers2

1

I also posted this question on github and I got my answer. In my case, I had some async code retrieving data from the server in the activate method of my child route's view model. The problem apparently came from the fact that the server didn't return before the binding engine was triggered so it took the information it could find (the parent's binding context).

I just had to return the promise from the activate method so aurelia would wait for the promise to return before launching the binding engine...

Gaet
  • 699
  • 3
  • 11
0

I guess this is working as intended as stated in the Docs:

The "scope" in aurelia is made up of two objects: the bindingContext (almost always a view-model instance) and the overrideContext which can be thought of as an "overlay" of the bindingContext. Properties on the overrideContext "override" corresponding properties on the bindingContext. It is actually rare for there to be a property on the overrideContext that is "hiding" a property on the bindingContext beneath. ...

See: http://aurelia.io/hub.html#/doc/article/aurelia/binding/latest/binding-how-it-works/3

Marc Scheib
  • 1,963
  • 1
  • 24
  • 29
  • I'm not sure about this. As I understand it, the override context is a separate object from the binding context. And in my case, when I use the Aurelia chrome extension, I can see the binding context of my child component (its value is correct) and the override context being empty... – Gaet Jun 27 '17 at 10:04
  • Perhaps this is true for when accessing the context in the VM, however, the view may only have access to the combination of both, the so-called overlay in the docs? – Marc Scheib Jun 27 '17 at 11:18
  • maybe.. I'm trying to get more info from the people from aurelia. I'll come back here to confirm/complete your answer when I get more details... Already thank you for your help! – Gaet Jun 27 '17 at 11:50