I didn't write but rather I'm reviewing the following Lightning JS function:
InitializeComponent : function (component) {
var recId = component.get("v.recordId");
var action = component.get("c.Init");
action.setParams({ "recordId" : recId });
action.setCallback(this, function(response) {
var resp = response.getReturnValue();
if (component.isValid()) {
if ($A.util.isEmpty(resp.RowData)) {
this.setErrorState(component, "No row data was returned for the Time Report");
return;
}
// move the root level node for the treeGrid
resp.RowData[0]["_children"] = resp.RowData[0]["us_children"];
delete resp.RowData[0]["us_children"];
// grab the root children and update the _children attribute for treeGrid requirements
for (let x of resp.RowData[0]["_children"]) {
x["_children"] = x["us_children"];
delete x["us_children"];
}
component.set("v.initResponse",resp);
this.HideSpinner(component);
}
},"SUCCESS");
action.setCallback(this, function(response) {
this.setErrorState(component, response.getError()[0].message);
},"ERROR");
$A.enqueueAction(action);
}
It populates a lightning:treeGrid component.
I'm confused about what's happening in this part:
// move the root level node for the treeGrid
resp.RowData[0]["_children"] = resp.RowData[0]["us_children"];
delete resp.RowData[0]["us_children"];
// grab the root children and update the _children attribute for treeGrid requirements
for (let x of resp.RowData[0]["_children"]) {
x["_children"] = x["us_children"];
delete x["us_children"];
}
again, resp = response.getReturnValue()
. Drilling down into the value returned from the server as far as possible, resp.RowData[0]
looks like this when debugging the server side action:
RowData:[BillableHours=37.50,
Name=Total,
NonBillableHours=0.5,
RowId=TOTALS,
TotalHours=null,
us_children=(RowData:[BillableHours=3.25,
Name=John Doe,
NonBillableHours=0,
RowId=John Doe,
TotalHours=null,
us_children=(RowData:[BillableHours=3.25,
Name=Project Management,
NonBillableHours=0,
RowId=John DoeProject Management,
TotalHours=null,
us_children=()])],
RowData:[BillableHours=33.25,
Name=John Doe2,
NonBillableHours=0,
RowId=John Doe2,
TotalHours=null,
us_children=(RowData:[BillableHours=32.75,
Name=Consulting Services,
NonBillableHours=0,
RowId=John Doe2Consulting Services,
TotalHours=null,
us_children=()],
RowData:[BillableHours=0.5,
Name=Project Management,
NonBillableHours=0,
RowId=John Doe2Project Management,
TotalHours=null,
us_children=()])],
RowData:[BillableHours=1.0,
Name=John Doe3,
NonBillableHours=0.5,
RowId=John Doe3,
TotalHours=null,
us_children=(RowData:[BillableHours=1.0,
Name=Consulting Services,
NonBillableHours=0,
RowId=John Doe3Consulting Services,
TotalHours=null,
us_children=()],
RowData:[BillableHours=0,
Name=Project Management,
NonBillableHours=0.5,
RowId=John Doe3Project Management,
TotalHours=null,
us_children=()])])]
Debugging the client side, resp.RowData[0]["_children"]
starts out undefined
and resp.RowData[0]["us_children"]
is:
So:
Does resp.RowData[0]["_children"] = resp.RowData[0]["us_children"];
actually, on the spot, create a "_children" item at the 0 index and simultaneously sets it equal to resp.RowData[0]["us_children"]
?
At any rate, what's going on in the loop?
And don't resp.RowData[0]["_children"]
and resp.RowData[0]["us_children"]
imply named indices - "_children" and "us_children" respectively? If so, I thought they weren't allowed in JS?