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?

  • This is really not clear - the callback function is triggered from your init function, so how it can be called before? – Itai Shmida May 14 '17 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 – erkan çipil May 15 '17 at 9:36
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.