3

SIMPLIFIED EXAMPLE

I want to find the dynamically created component "test2":

Markup

<aura:component access="global"  
    implements="flexipage:availableForAllPageTypes,force:hasRecordId,force:hasSObjectName" 
>
    <aura:handler name="init"           value="{!this}"                 action="{!c.init}" />
    <ui:button label="Find" press="{!c.find}"/>
    {!v.body}
    <ui:button aura:id="test2" label="test2" />
</aura:component>

Controller

({
    init : function(cmp, evt, hlp) {
            $A.createComponent(
                "ui:button", {
                    "aura:id"       : "test1",
                    "label"         : "test1",
                },
                function(newButton, status, errorMessage){
                    if (status === "SUCCESS") {
                        var body = cmp.get("v.body");
                        body.push(newButton);
                        cmp.set("v.body", body);
                    } 
                }
            ); 
    },
    find : function(cmp, evt, hlp) {
        console.log( 'but1' , cmp.find("test1") );
        console.log( 'but2' , cmp.find("test2") );
    },
})

Result

enter image description here


ORIGINAL EXAMPLE

I have a component using a

<ui:button label="Save" press="{!c.save}"/>
<force:recordEdit aura:id="recordEdit1" recordId="{!v.itemId}" />

in it's markup. There this method to save:

save : function(cmp, evt, hlp) {
    cmp.find("recordEdit1").get("e.recordSave").fire();
    console.log( cmp.find("recordEdit1") );
},

Now when I'm when I remove force:recordEdit from the markup and create the force:recordEdit dynamically, all seems to work fine with this in the doInit():

$A.createComponent(
       "force:recordEdit", {
            "aura:id"       : "recordEdit1",
            "recordId"      : cmp.get("v.itemId"), 
        },
        function(newButton, status, errorMessage){
                if (status === "SUCCESS") {
                    var body = cmp.get("v.body");
                    body.push(newButton);
                    cmp.set("v.body", body);
                }
        }
); 

However in save() this line does not find the component anymore:

cmp.find("recordEdit1").get("e.recordSave").fire();

Why this?

1

0

Browse other questions tagged .