I am creating some dynamic components inside a lightning component. They are created on the click of a button. The code seems to executes or render differently when the button is clicked first compared to subsequent clicks. The subsequent clicks render the component correctly. Using console.log - it looks like the code is executing differently on each click even though the exact same code is running (I have even take out the apex to strip the code down to hard coded lists to find where the issue is)
Lightning Component Helper
createDynamicComps : function(component, event) {
component.set('v.body', []);
var LevelOne = ['BMW','Audi','Jeep'];
var LevelTwo = ['Black','Green','Red','Blue','Purple'];
for (var leveloneIterator = 0; leveloneIterator < LevelOne.length; leveloneIterator++){
$A.createComponent("ui:message", {
"Title": 'test',
"severity" : "message"
},
function(newCmp, status, errorMessage){
var body = component.get("v.body");
body.push(newCmp);
component.set("v.body", body);
console.log('UI Message');
}
);
for (var leveltwoIterator = 0; leveltwoIterator < LevelTwo.length; leveltwoIterator++){
$A.createComponent("ui:inputRadio", {
"aura:id": LevelTwo[leveltwoIterator].replace(" ","")+leveltwoIterator,
"name": LevelTwo[leveltwoIterator].replace(" ",""),
"label": LevelOne[leveloneIterator] + " Color: " + LevelTwo[leveltwoIterator]
},
function(newCmp, status, errorMessage){
var body = component.get("v.body");
body.push(newCmp);
component.set("v.body", body);
console.log('UI Radio');
}
);
}, ...
This is called by the controller.The controller code is called when a button is clicked
doButtonStuff : function(component, event, helper) {
helper.createDynamicComps(component, event);
},
When the button is clicked the first time - The four UI Messages display and then 15 radio buttons.
When the button is clicked subsequent times - a message is displayed and then five radio buttons below it for each message. This execution is also shown in the console screen shot below