0

I'm creating a component that, based on a custom fieldset, generates a form. The point is when is trying to create lightning:select, lightning:duallistbox and lightning:textarea the $A.createComponent launches after everything else is created even though the call for the function is made at the right time:

My function createTextArea works properly with lightning:inputRichText elements but not with textareas:

createTextArea: function(component, field, data){
    var result = component.get("v.simpleRecord");  
    //Standard textarea
    if(data.type === "textarea"){
        console.log("entra create textarea");
        $A.createComponent(
            "lightning:textarea",
            data,
            function(newCmp, status, errorMessage){
                if (status === "SUCCESS") {
                    var cmpArray = component.get("v.fieldSetBodyFull");
                    cmpArray.push(newCmp);
                    component.set("v.fieldSetBodyFull", cmpArray);
                    console.log("entra full textarea: " + component.get("v.fieldSetBodyFull"));
                } else if (status === "INCOMPLETE") {
                    console.log("ERROR", "Error creating the component with missing fields.")
                } else if (status === "ERROR") {
                    console.log("ERROR", errorMessage);
                }
            }
        );
    }
    //Textarea HTML Formatted
    if(data.type === "richtext"){
        console.log("entra create richtext");
        $A.createComponent(
            "lightning:inputRichText",
            data,
            function(newCmp, status, errorMessage){
                if (status === "SUCCESS") {
                    var cmpArray = component.get("v.fieldSetBodyFull");
                    cmpArray.push(newCmp);
                    component.set("v.fieldSetBodyFull", cmpArray);
                    console.log("entra full richtext: " + component.get("v.fieldSetBodyFull"));
                } else if (status === "INCOMPLETE") {
                    console.log("ERROR", "Error creating the component with missing fields.")
                } else if (status === "ERROR") {
                    console.log("ERROR", errorMessage);
                }
            }
        );
    } 
},

And as you can see with the console, it's getting launched after everything else is made:

enter image description here

Does anyone know how to fix this?

EDIT:

How can I implement promises to make it work? Cause it seems that the best solution for me.

  • mind includng your components markup? (.cmp) – glls Jul 16 '18 at 12:01
  • AFAIK there are no order guarantees in $A.createComponent calls. So if, for example, the lightning:inputRichText component was already cached in the browser but the lightning:textarea wasn't (so it had to be fetched), the after creation callback of the lightning:textarea would very likely come later than you might expect. If you need order guarantees (best to avoided) you would have to code that yourself using e.g. Promises or make a single create call for multiple components that won't complete until all the components have been downloaded and created. – Keith C Jul 16 '18 at 13:47
  • Thanks for the answer! And how do you implement promises into my code for textarea? Cause I need order guarantees and I've read before about promises but don't know how to put them in my code.. @KeithC – molinet Jul 16 '18 at 13:51
  • @molinet Suggest you add that to your question if you think its the way to go. – Keith C Jul 16 '18 at 13:56
  • Already added. Help would be appreciated! @KeithC – molinet Jul 16 '18 at 14:12

Your Answer

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

Browse other questions tagged or ask your own question.