You need to sign in to do that
Don't have an account?
Nethra Raghupathy
$A.createComponent() inside callback
Hi,
I need to render an existing component with attribute based on the output from an API call (callback).
FirstController.js
Based on action1 testOne.cmp gets created.
testOne.cmp
I have the endpoint attribute defined in component
testOneController.js:
testOne component gets rendered in 'currentContent' but the endpoint attribute is not getting passed in $A.createComponent.
Is there a way to use $A.createComponent inside an API callback?
I need to render an existing component with attribute based on the output from an API call (callback).
FirstController.js
init: function (component) { component.set('v.oppId','25978371'); component.set("v.userId", $A.get("$SObjectType.CurrentUser.Id")); var orgDetailsAction = component.get("c.getLocalAPI"); orgDetailsAction.setCallback(this, function(data) { console.log(data.getReturnValue()) data = JSON.parse(data.getReturnValue()); if(!data.result){ component.set('v.sidebar','disabled'); component.set("v.domainPopup", true); console.log(component.get("v.domainPopup")) } else{ if(data.api1==null) component.set('v.sidebar','disabled'); else component.set('v.SFDCEndpoint',data.api1); } var action1 = component.get("c.getExternalAPI"); action1.setCallback(this, function(data) { var state = data.getState(); var role = JSON.parse(data.getReturnValue()).data; component.set("v.userRole", role); console.log(component.get('v.SFDCEndpoint')) $A.createComponent(component.get("v.sidebar")!='disabled' ? ((component.get("v.userRole")=='A1'||component.get("v.userRole")=='A2') ? "c:testOne":"c:testTwo") : "c:testThree",{ endpoint: component.get('v.SFDCEndpoint'), }, function(newCmp,status, errorMessage){ if(status === "SUCCESS"){ if (component.isValid()) { component.set("v.currentContent", newCmp); }} } ); }); $A.enqueueAction(action1); }); $A.enqueueAction(orgDetailsAction); },
Based on action1 testOne.cmp gets created.
testOne.cmp
I have the endpoint attribute defined in component
<aura:handler name="init" value="{!this}" action="{!c.onLoad}"/> <aura:attribute name="endpoint" type="String"/>
testOneController.js:
onLoad : function(component, event, helper) { console.log(component.get('v.endpoint')); }
testOne component gets rendered in 'currentContent' but the endpoint attribute is not getting passed in $A.createComponent.
Is there a way to use $A.createComponent inside an API callback?
I can suggest to use the promises
..Refer this this link
https://rajvakati.com/2018/05/29/using-promise-in-lightning-component/