3

I'm building a component structure of the kind

<c:parent>
   <c:child name="xyz" />
</c:parent>

and I need the parent to know about its children. Why, because the parent is displaying something for all the children. So I need it BEFORE that parent initializes itsself.

I thought I can do that by firing events on child initialization

<aura:component >
    <aura:attribute name="name" type="Integer" required="true" />
    <aura:registerEvent name="initialized" type="c:registerEvent" />
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
</aura:component>

and handling them in the parent

<aura:component >
    <aura:attribute name="children" type="Object[]" default="[]" />

    <aura:handler name="initialized" event="registerEvent" action="{!c.register}"/>
    <aura:handler name="init" value="{!this}" action="{!c.init}" />
</aura:component>

Controller:

({
    register : function(component, event, helper) {
        var children = component.get("v.children");
        children.push({ "name": event.getParam("name") });
        component.set("v.children", children);           
    },

    init: function(component, event, helper) {
        var children = component.get("v.children");
        console.log("init " + children);
    }
})

Register never seems to be called in the parent. Maybe because the parent can't handle events before itsself is inititalized. But that would be to late.

How can I solve this? I read this great article Lightning Inter-Component Communication Patterns about the topic but couldn't find an answer.

UPDATE: The solution can be found in this question Lightning component event not fired or handled and its answer.

4
  • You are building a list of children? Are you going to display their names or something similar? Why not do it in the renderer and override the afterrender function? Why would that be too late? Jun 23, 2017 at 5:17
  • I am building a modal with a setup wizard where each step child component knows what to display per step but the wrapping wizard component is showing a progress indicator with all steps. Therefore it needs to know them oninit. Can you show code please? Jun 23, 2017 at 5:48
  • I'm thinking about it... will look again after tea Jun 23, 2017 at 5:51
  • Strangely enough, the thing I found in the framework last night might help... - you could use session storage (provided by custom value provider) - check out sfdcfox's answer to my question here: salesforce.stackexchange.com/questions/180007/… Jun 23, 2017 at 5:54

2 Answers 2

1

I would try to trigger an event from your outer component: "Hi there, I'm initialized". So now your inner components know they can register them self to the parent.

2
0

How about creating the children dynamically with $A.createComponent()? https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/js_cb_dynamic_cmp_async.htm

Would that solve your issue?

1
  • No that would not be of any help. The child components need to be in the *.cmp as they define the body markup. Jun 23, 2017 at 12:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .