Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
I have a lightning component that includes an apex class to return a string. I would like the string that is returned to set a value in an event listener in the component's doInit function.

I can tell that communityURL is eventually getting set with the right string, so my connection to the apex class is working. However, I believe because I'm using $A.enqueueAction(action), it's happening AFTER I've already tried to set vfOrigin. So my vfOrigin is null, instead of the returned string. But if I don't use $A.enqueueAction(action), it doesn't appear to call the apex class at all.

Any suggestions for what I'm missing?

 

({

doInit : function(component, event, helper) {

// create a one-time use instance of the getCommunityURL action

// in the server-side controller

var action = component.get("c.getCommunityURL");

console.log("[mf] action: " + action);

var communityURL = '';

action.setCallback(this, function(response){

var state = response.getState();

if (state === "SUCCESS"){

// alert the user with the value returned from the server

alert("From server: " + response.getReturnValue());

communityURL = response.getReturnValue();

} else {

console.log(state);

}

});

$A.enqueueAction(action);

// this section handles the reCaptcha that is in the embedded VF page

// when the reCaptcha passes, make the submit button clickable

// TODO: update with correct origin for production

//let vfOrigin = "https://mflei-eventbritecommunity.cs61.force.com";

alert('[MF] communityURL: ' + communityURL);

let vfOrigin = communityURL;

window.addEventListener("message", function(event) {

console.log(event.data);

if (event.origin !== vfOrigin) {

// Not the expected origin: Reject the message!

alert('vfOrigin: ' + vfOrigin);

alert('Not the expected origin: ' + event.origin);

return;

}

if (event.data==="Unlock"){

let myButton = component.find("myButton");

myButton.set('v.disabled', false);

}

}, false);

},

 
2 answers
  1. Jul 24, 2018, 5:41 AM
    Hi Margaret,

    Try something like below.

    ({

    doInit : function(component, event, helper) {

    // create a one-time use instance of the getCommunityURL action

    // in the server-side controller

    var action = component.get("c.getCommunityURL");

    console.log("[mf] action: " + action);

    var communityURL = '';

    action.setCallback(this, function(response){

    var state = response.getState();

    if (state === "SUCCESS"){

    // alert the user with the value returned from the server

    alert("From server: " + response.getReturnValue());

    communityURL = response.getReturnValue();

    alert('[MF] communityURL: ' + communityURL);

    let vfOrigin = communityURL;

    window.addEventListener("message", function(event) {

    console.log(event.data);

    if (event.origin !== vfOrigin) {

    // Not the expected origin: Reject the message!

    alert('vfOrigin: ' + vfOrigin);

    alert('Not the expected origin: ' + event.origin);

    return;

    }

    if (event.data==="Unlock"){

    let myButton = component.find("myButton");

    myButton.set('v.disabled', false);

    }

    }, false);

    } else {

    console.log(state);

    }

    });

    $A.enqueueAction(action);

    },

    })

    regards

    Sampath(Sampathjt@gmail.com)

     
0/9000