bindingaureliabinding-context

aurelia bindingcontext gets overridden by parent


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...


Solution

  • 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...