[SalesForce] Lightning Components – Are passed in attributes available to the init method

All,

This may be a basic question, but I've Googled around it and haven't found an explicit piece of documentation to back it up.

I have a Lightning component with a required attribute which is a list of Custom Object

<aura:attribute name="example" type="Custom_Object__c[]" required="true" default="[]"/>

This component has an init handler which takes the list of Custom objects and effectively turns them into a wrapper class (so I can add a tickbox to each record in the list).

I use this Component [CHILD] in the body of another component [PARENT] (and that component passes in the list of SObjects)

Weirdly, the init on the CHILD component doesn't seem to know about the passed in list of SObjects. It thinks it has an empty list, so it has nothing to iterate through. This surprised me as I had assumed that all of the passed in attributes would be available to the init method.

Is there documentation anywhere which explains the order in which the init handler and the passed in attributes, etc are initialised/kicked-off?

With thanks,
Andy

Best Answer

init event of the child component will called long before the parent's. Add aura:doneRendering event in the child component and access the list in in the child. Order in which init fired is mentioned in the developer guide and in this article too.

When the component tree is ready, the init event is fired for all the components, starting from the children component and finishing in the parent component.

Beware aura:doneRendering will be called multiple times as part of the component lifecycle, when the aura:attributes are dirty. So have a aura:attribute which serves as a flag to help, you decide whether to process the logic.