[SalesForce] Required attribute and init handler in lightning component

I have the following in my component:

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="item" type="Camping_Item__c" required="true" />

Component's controller simply calls apex auraEnabled method (which is fetches one record of Camping_Item__c type) and sets v.item attribute.

When I try to run an app with this component, I get an error:

[NoErrorObjectAvailable] Aura.loadComponent(): Failed to initialize
application.

If I remove required="true" from aura:attribute all goes fine.

Why is that? Is it possible to use the required="true" property with init handler?

Best Answer

I am pretty sure that if you set required="true" then the app or component that declares your component needs to provide the item. For example if your component is called "Camponent" in namespace "c" then the parent app/component would need to have:

<c:Camponent item="{!v.someCampingItemThatAlreadyExists}" />

Now, if the parent's init method fills in that value, then you're fine because it won't even attempt to create the c:Camponent until the init method has finished.

Related Topic