I'm trying to call functions in the right order in my lightning component. First, I would like to call doInit, then, I would like to trigger a function after the script is loaded. How can I achieve this?
component.cmp
<aura:component>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<ltng:require
scripts="{!$Resource.myCustomJsLibrary}" afterScriptsLoaded="{!c.scriptsLoaded}"
/>
</aura:component>
controller.js
({
doInit: function(cmp){
var action = cmp.get("c.getValueFromDatabase");
action.setCallback(this, function(response){
var state = response.getState();
if (state === "SUCCESS") {
cmp.set("v.settings", response.getReturnValue());
}
});
$A.enqueueAction(action);
},
scriptsLoaded : function (cmp) {
cmp.get("v.settings");
}
})
c.scriptsLoaded
which will execute once the scripts have been loaded. Here is the order or execution: Order of execution – itzmukeshy7 May 1 '17 at 11:27settings
attribute? but here it's not sure that the value ofsettings
is available because we are making anasynchronous
request to fetch data from the server which may take some time and you will get the default value. – itzmukeshy7 May 1 '17 at 11:35