I have a lightning component like below Dynamic.cmp:
<aura:component controller="Mobile_Controller">
<aura:attribute name="testDate" type="String"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
{!v.body}
</aura:component>
and its controller
doInit: function(component, event, helper) {
var action = component.get("c.getDay");
action.setParams({newDate: component.get("v.testDate")});
action.setCallback(this, function(a) {
if (a.getState() === "SUCCESS") {
console.log('Call back in dynamic component !');
if(component.isValid()) {
console.log(a.getReturnValue());
component.set('v.body',a.getReturnValue());
}
}
});
$A.enqueueAction(action);
},
And i would like to create this component in another components action, but callback function returns before child component's init action is completed. My main component:
<aura:component controller="Mobile_Controller2">
<div aura:id="dynamic">
</div>
<a href="javascript:void(0)" onclick="{!c.createComponent}">Create</a>
</aura:component>
Its controller:
createComponent: function(cmp, event, helper){
$A.createComponent(
"c:Dynamic", {
"testDate" :"test"
},
function(cmp2, status, errorMessage) {
console.log(status);
if (status === "SUCCESS") {
var dynamicDiv= cmp.find('dynamic');
var body = dynamicDiv.get('v.body');
dynamicDiv.set('v.body', cmp2);
}
}
);
}
So it shows empty, because createcomponent action does not wait to complete init actions. How could i accomplish this scenario?