3

I can pass arguments or I can pass a callback to my aura method but the syntax for passing both an argument and a callback at the same time eludes me. I would like to pass the ultimateParentID var along with the callback.

My working calling method in my parent controller:

({
    //get facilities list by ultimate parent 
    initFacilities: function (component, event, helper) {
        var formData = component.find("childCmp");
        var ultimateParentID = component.get("v.ultimateParentID");
        formData.getFacilities(function(response) {
          console.log('number of facilities for select is: ' + response.length);
            var opts = [];
            opts.push({
                value:"",
                label:"--- None ---"
            });
            for (var i = 0; i < response.length; i++) {
                opts.push({
                value: response[i].Id,
                label: response[i].Alias__c
                });
            }
            component.set("v.facilityOptions", opts);
          });
    }

My aura method definition in my child component, where the callback and ultimateParentID are parameters are defined as attributes:

<aura:component controller="VOBRequestLightningFormDataController"> 
   <aura:method name="getFacilities" action="{!c.getFacilities}" description="get facilities in System mode" access="PUBLIC"> 
       <aura:attribute name="callback" type="Function" />
       <aura:attribute name="ultimateParentID" type="String" default="0013700000Hsz8f"/>
    </aura:method> 
</aura:component>

Child controller:

({
    getFacilities : function(cmp, event) {
        var params = event.getParam('arguments');
        var callback;
        if (params) {
            var ultimateParentID = params.ultimateParentID;
            //alert("parent is: " + ultimateParentID);
            callback = params.callback;
        }
        var action = cmp.get("c.fetchData");
        action.setParams({
            "ultimateParentID" : ultimateParentID 
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                console.log("From server: " + response.getReturnValue());
                // return doesn't work for async server action call
                //return response.getReturnValue();
                // call the callback passed into aura:method
                if (callback) callback(response.getReturnValue());
            }
            else if (state === "INCOMPLETE") {
                // do something
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " +
                          errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });
        $A.enqueueAction(action);
    }

As written above, the callback works. I've tried the following in the parent controller calling method, which ends up assigning the Callback reference to the ultimateParentID variable:

formData.getFacilities(ultimateParentID, function(response) {...

And, if I just call formData.getFacilities(ultimateParentID) without the callback, the argument is passed successfully. Thanks in advance for your help.

1

Method parameters need to be called in the correct order.

Change your call to:

formData.getFacilities( function(response){...}, ultimateParentID );

Or redefine your params in the component as:

<aura:method name="getFacilities" action="{!c.getFacilities}" description="get facilities in System mode" access="PUBLIC"> 
   <aura:attribute name="ultimateParentID" type="String" default="0013700000Hsz8f"/>
   <aura:attribute name="callback" type="Function" />
</aura:method> 
| improve this answer | |

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.