4
doInit : function(component, event, helper) {
    helper.method1(component);
    helper.method2(component);
    helper.method3(component);
}

Need to call these helper methods in lightning client side controller synchronously.

Component markup:

Here's my code:

<aura:component controller="EmpAttachmentListController"> 
<aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 
<aura:attribute name="empList1" type="Employee__c[]" /> 
<aura:attribute name="empList2" type="Employee__c[]" /> 
<table>
    <tr>
        <th>Employee Name</th>
        <th>Imgs Count</th> 
    </tr>
    <aura:iteration indexVar="index" var="emp" items="{!v.empList1}"> 
        <tr>
            <td>{!emp.Employee_Name__c}</td>
            <td>
                {!emp.Attachments.le‌​ngth} Imgs <a href="#" onclick="{!c.toggleRecord}" id="{!index}">show/hide</a>
            </td>
        </tr>
        <tr>
            <td>
                <p id="{!'index'+index}" class="showP">
                <aura:iteration var="attach" items="{!emp.Attachments}"> 
                    <img src="{!'/servlet/servlet.FileDownload?file='+attach.Id}" /> 
                </aura:iteration>
                </p>
            </td> 
        </tr>
    </aura:iteration>
</table>
</aura:component>

controller.js

({ doInit : function(component, event, helper) 
    { 
        helper.getEmpList(component); 
    }
});

helper.js

({ 
    getEmpList: function(component) { 
        var that = this; 
        var action = component.get("c.getEmpList"); 
        action.setCallback(that, function(result) { 
            component.set("v.empList", result.getReturnValue());
            that.getEmpList2(component); 
        }); 
        $A.enqueueAction(action);
    }, 
    getEmpList2: function(component) { 
        var that = this; 
        var action = component.get("c.getEmpList2"); 
        action.setCallback(that, function(result) {
            var c = component.find("index0"); ** //Getting undefined error here ** 
            component.set("v.empList2", result.getReturnValue()); 
        }); 
        $A.enqueueAction(action); 
    } 
})
1
  • you can call them synchronously like above. do they have any code to invoke controller method?
    – Praveen
    Sep 14, 2016 at 7:49

1 Answer 1

14

If there's no code in the helper method to invoke apex controller method(@auraenabled), you can call the methods one by one, since they are executed synchronously.

controller.js

doInit:function(component){
    helper.method1(component);
    helper.method2(component);
    helper.method3(component);
}

helper.js

({
    method1:function(cmp){
        // do some stuff
    },
    method2:function(cmp){
        // do some stuff
    },
    method3:function(cmp){
        // do some stuff
    }
})

But if you have to invoke @auraenabled method in the helper method, then you need to chain the methods because @auraenabled callbacks are asynchronous:

controller.js

doInit:function(component){
    helper.method1(component);
}

helper.js

({
    method1:function(cmp){
        var action = cmp.get("c.callingMethod1");
        var self = this;
        action.setCallback(this,function(resp){
            // do some stuff and call method2
            self.method2(cmp);
        });

        $A.enqeueAction(action);
    },
    method2:function(cmp){
        var action = cmp.get("c.callingMethod2");
        var self = this;
        action.setCallback(this,function(resp){
            // do some stuff and call method2
            self.method3(cmp);
        });

        $A.enqeueAction(action);
    },
    method3:function(cmp){
        var action = cmp.get("c.callingMethod3");
        var self = this;
        action.setCallback(this,function(resp){
            // do some stuff
        });

        $A.enqeueAction(action);
    }
})
8
  • Then chain methods in the actions callback as described in the above answer
    – Praveen
    Sep 14, 2016 at 9:33
  • @Ajay component.find() can take only aura:id not html id attribute.Join this room to discuss about it.
    – Praveen
    Sep 14, 2016 at 10:25
  • I have modify them as aura:id still getting undefined error. And chat is not enabled for me.
    – Ajay
    Sep 14, 2016 at 10:33
  • you cannot dynamically set aura:id as you do in for html id attribute inside aura:iteration.can you check the chat now?
    – Praveen
    Sep 14, 2016 at 10:35
  • As I have no reputation. I am unable to chat you.
    – Ajay
    Sep 14, 2016 at 10:41

You must log in to answer this question.

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