I try to set a value in my lightning component via the helper function, but it does not set the value in the component, and I get the wrong value when I try to do component.get in the controller. Here is my code:
Test.cmp
<aura:component controller="TestController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,lightning:isUrlAddressable" access="global" >
<aura:attribute name="consoleNavigation" type="Boolean" default="false" access="global"/>
<aura:handler name="init" value="{!this}" action="{!c.init}"/>
</aura:component>
TestController.js
({
init : function(component, event, helper) {
helper.isConsoleNavigation(component, event, helper);
var consoleNavigation = component.get("v.consoleNavigation");
console.log('consoleNavigation '+consoleNavigation);
}
})
TestHelper.js
({
isConsoleNavigation : function(component, event, helper) {
var workspaceAPI = component.find("workspace");
workspaceAPI.isConsoleNavigation().then(function(response){
component.set("v.consoleNavigation",response);
console.log('isConsoleNavigation '+response);
})
.catch(function(error) {
console.log('isConsoleNavigation Error '+error);
});
}
})
In the helper, the value gets logged properly (true if used in the Service Cloud and on Case record page), but in the controller the initialized value does not get changed. Does that have something to do with JavaScript assigning variables first? But how would I work around that? Thanks!
.then()
means that is returning a promise, which the synchronous part of your code doesn't like. try returning a new Promise on the helper and putting your rest of the code of the controller in a.then