I have a bunch of components <c:CmpA />
, <c:CmpB />
…
Sometimes, I need to perform a check for access rights to this component, so I decided to create a <c:CheckWrapper></c:CheckWrapper>
as this:
<aura:component description="CheckWrapper"
controller="CheckWrapperController"
>
<aura:if isTrue="{! not(v.loading) }">
<aura:if isTrue="{! v.isAllowed }">
{! v.body }
<aura:set attribute="else">
{! v.error }
</aura:set>
</aura:if>
</aura:if>
</aura:component>
Its JS controller perform a check on Apex, and then release v.isLoading
and v.isAllowed
(or not).
The usage is has this:
<c:CheckWrapper somethingToCheck="['this', 'that']">
<c:CmpA attr="{! v.attr }" />
</c:CheckWrapper>
Everything works perfectly except that even if you are not allowed to use this component (and it will not be mounted on the DOM because of <aura:if isTrue="{! v.isAllowed }">
), it is instantiate and its <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
is performed.
How can I prevent this without dynamically creating the component ? As an example, <lightning:Tab>
and <lightning:Tabset>
work exactly the same way but the child component's init handler is performed on render.
<lightning:tabset selectedTabId="{! v.stepName }" >
<lightning:tab label="label" aura:id="id" id="id" >
<c:CmpA attr="{! v.attr }" />
<!-- Tab is loaded, component is mounted, init is performed -->
</lightning:tab>
<lightning:tab label="label2" aura:id="id2" id="id2" >
<c:Cmpb attr="{! v.attr }" />
<!-- Tab is hidden, component is unmounted, init is not performed until open the tab -->
</lightning:tab>
</lightning:tabset>
Do you have any idea I can have the same behaviour ?
Best Answer
Can you try this inside your CheckWrapper Component.
Edit Solution 2:
Here you can pass your component as an attribute to checkWrapper and let checkWrapper decide whether to show or not components.
Test App Code
CheckWrapper.cmp
CheckWrapperController.js
CheckWrapperHelper.js
CmpAController.js
CmpA.cmp
You can change the default value of isAllowed to true on checkWrapper to check component creation. Hope this helps!!