6

I am using lightning:overlayLibrary to collect input from the user which will then be used in the parent component. The modal is simple: 1 lightning:select` in the body and a footer with "Select" and "Cancel" buttons. The only way I see to accomplish this is to:

  • fire event from an onchange handler in the body component that sends the selected value
  • handle change value event in footer
  • fire event from onclick event on "Select" button which sends selected value
  • handle said event in parent component

Is this the best way to do this? I tried messing with the closeCallback parameter in the showCustomModal function, but it appears that the attributes have already been wiped by that point (if you can access them at all).

1
  • 2
    if the below answer is correct, please mark it as the accepted answer – Robs Nov 14 '18 at 23:01
10

There's another way to do this. You can bind an attribute in your parent component to an attribute in the modal body:

Create an attribute on the parent and on the body component.
In the controller function where you open the modal and generate the body and footer, pass a reference to the parent attribute to the body attribute. When you change the value of the modal body attribute, the value will be passed to the parent attribute through this binding.
Now you simply have to fire an onclick event to notify the parent that the Select button is clicked and to have it handle its logic. There is no need to send any parameters in the event.

$A.createComponents(
            [
                ["c:modalBody",
                    {
                        modalBodyAttributeName : component.getReference('v.parentAttributeName')
                    }],
                ['c:modalFooter', {}]
            ],
            function(contents, status, errorMessage) {
                if (status === "SUCCESS") {
                    modalBody = contents[0];
                    modalFooter = contents[1];
                    component.find('overlayLib').showCustomModal({
                        header: "header title",
                        body: modalBody,
                        footer: modalFooter,
                        showCloseButton: true,
                        closeCallback: function() {

                        }
                    });
                } else {
                    console.log(errorMessage);
                }
            }
        );
1
  • 1
    This is an EXCELLENT solution. I wish I were the OP so I could mark it as the accepted solution. – Sebastian Kessel May 31 '18 at 19:59

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.