Reputation: 689
I have a parent
component where the view model contains a property prop1
.
In its view, a custom element has a view-model.ref="prop1"
.
export class Parent {
public prop1;
}
<template>
<custom-element view-model.ref="prop1"></custom-element>
</template>
This works like a charm and I get a reference to the view model of my custom element in my parent
.
Now, I add a child router to parent
with a child
component. In child
's view model, I add a property prop1
. and in its view, a custom element has a view-model.ref="prop1"
. So exactly like I did in parent
...
As soon as I navigate to parent/child
, the parent
's container prop1
stops referencing the custom element of parent
and starts referencing the one from child
.
If I name the properties differently, there is no problem.
Any idea why this happens? And how could I avoid this behavior without worrying about the naming of the properties?
EDIT I chanced upon some more information! If the properties are initialized in the view model, I seem to be able to retain the references in the view models. Note that I'm using Typescript so I think the compiled code for an unassigned class property doesn't mention the property at all until it is assigned. I still don't really understand where the problem comes from exactly...
And I remain with the same problem if I use a view-model.ref
directly in the template without mapping it to an explicit property from the view model like this:
<template>
<custom-element view-model.ref="custom"></custom-element>
<custom-element2 opened.call="custom.opened()"></custom-element2>
</template>
Upvotes: 2
Views: 289
Reputation: 14094
when you create a property in a class and don't assign anything to it, babel/typescript will remove that property as if it was not even declared.. because it's really not doing anything. typescript is interested in your definitions in compile time only.
now that your property in the child is emitted, you have a binding in the child to an undeclared property.
in that case, aurelia
creates a property for you and bind to it..
but in your case, aurelia finds this property in the parent scope (just like regular JS scoping rules), and therefor do not create a "new property" in the child scope but rather binds to the parent property.
this is why when you initialize the property in the child (even with undefined
) it "works". because you have 2 properties in 2 scopes.
and when you change the name, aurelia will create a new property for you.. and here come another rule - wen aurelia create a property for you - it creates it in the lowest scope available.. in your case - the child scope. and everything works again.
you can notice this behavior a lot with repeate.for
, because the repeater creates an invisible scope for each repeate
loop..
if you bind anything in the repeate to a property that dont exists, you will have that property in each child, and not once in the parent.
Upvotes: 2