2

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?

2
  • This is really not clear - the callback function is triggered from your init function, so how it can be called before? May 14, 2017 at 8:47
  • I dont know what it is not clear from your side but i did a wrong binding on my dynamically created component, that is why it showed blank. So the problem has been resolved. @ItaiShmida May 15, 2017 at 9:36

1 Answer 1

-1

Actually, everything is working as expected, i just made a wrong binding in my dynamic component , that is why i was showing blank. My Bad!

You must log in to answer this question.

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