I want to call the init method of a child LC from a parent LC.
I tried to use aura:method, and call the init method from a parent method.
.....
<aura:attribute name="userContact" type="Contact" />
<aura:method name="callInit" action="{!c.init}" >
<aura:registerEvent name="appEvent" type="c:ET_AppEvent_OverNightStay"/>
<aura:handler name="init" value="{! this }" action="{! c.init }"/>
<lightning:layout multipleRows="false" >
<lightning:layoutItem size="10">
</lightning:layoutItem>
........
But it seems, that if I use (aura:method) I can't use (aura:registerEvent) and (lightning:layout) and many other tags!!
So my Qeustion:
How can I achieve this?
Just to clearify my problem, I have a parent component, and in this parent LC I have several Child LCs. It's like a path that user has to take.
this path is the following, when a user click on (event reservation) he has to fill the following (Select Contacts –> Select Events to attend –> Select Hotel and Room –> Summary&Confirm –> Confirmation Message)
There is a big parent then (book) parent then the 5 childs.
In General I'm using the (Lightning Inter-Component Communication Patterns)
– To send data from parent to child –> Attributes
– To send data from child to event –> Events
So when ever I'm switching to the next step (LC) the init method will be fired (but only once!).
So when I'm on the second step and select an event, then click next and fill the data of the hotel third step. If I click back to second step, and changed something and clicked next, the data from the second step, will not be noticed, since the init will not be fired again.
Best Answer
As you've mentioned above, if you are able to catch the tab switching activity in parent component and based on that call the
aura:method
, this should be working fine and should not give you problem ofaura:registerEvent
and other tags not rendering.Recommended as this will not require lot of code re-structuring and work with your existing implementation:
Continue with your existing implementation and find out syntax issue if any.
Use Renderer that calls same code as init():
Move your init code in a helper method. Call this helper ,method from
afterRenderer()
in renderer file of lightning component bundle.This way, you won't need to call
init()
explicitly usingaura:method
and everytime the component is rendered, your required code would be called.afterRenderer()
may be called multiple times if any DOM is changed causing unnecessary execution of code. This will then require to add logic to avoid the code repetition.Inheritance in lightning components