The code that acts as an intermediary between the two handlers is an Infobus, which is an EGL library in the com.ibm.egl.rui project.
You begin this lesson by dealing with the second of those two steps. You update the previously written MortgageCalculatorHandler handler to invoke the Infobus publish function when a new calculation is returned from the remote service. Then, you ensure that the CalculationResultsHandler handler has subscribed to the event.
The publish-and-subscribe makes possible the pie-chart display.
InfoBus.publish("mortgageApplication.mortgageCalculated", retResult);
The first argument is the event name, and the second is
the record being passed to the functions that are registered for that
event. Recall that the structure of that record is as follows:record MortgageCalculationResult
// user input
loanAmount money;
interestRate decimal(10, 8);
term int;
// calculated fields
monthlyPayment money;
interest money;
end
An error mark is displayed because the Infobus library is not being imported. To add the required import statement, press Ctrl+Shift+O. To remove the error mark, save the file.
function displayResults(retResult MortgageCalculationResult in)
paymentLabel.text = MortgageLib.formatMoney(retResult.monthlyPayment as STRING);
inputRec_form.publish();
hideProcessImage();
InfoBus.publish("mortgageApplication.mortgageCalculated", retResult);
end
As before, this code shows the payment amount in the payment field and uses the Rich UI MVC mechanism to publish the results of the calculation in the retResult record. The new statement involves a different kind of publish, making the record available to any widget that subscribes to the mortgageApplication.mortgageCalculated event.
interestPieChart
EGL displays a default pie chart.
new PieChartData{ y=1, text="Principal", color="#99ccbb"},
new PieChartData{ y=0, text="Interest", color="#888855"}
To
calculate how much of the pie chart is taken by a given record, divide
the y-field value for that record by the sum of y-field
values. In this case, the division is 1/1, and the initial display
shows the mortgage principal at 100%. The display at development time
is only a placeholder until the application handles the first, default
calculation at run time.InfoBus.subscribe("mortgageApplication.mortgageCalculated", displayChart);
This
code ensures that the Infobus invokes the displayChart function
whenever the specified event occurs. The next step will remove the
error marks.function displayChart(eventName STRING in, dataObject ANY in)
localPieData PieChartData[2];
resultRecord MortgageCalculationResult =
dataObject as MortgageCalculationResult;
localPieData = interestPieChart.data;
localPieData[1].y = resultRecord.loanAmount;
localPieData[2].y = resultRecord.interest;
interestPieChart.data = localPieData;
end
When the event occurs, the displayChart function
receives the input data into the dataObject parameter.
The use of ANY as the parameter type ensures that you can use the
Infobus mechanism to transfer any type of record. resultRecord MortgageCalculationResult =
dataObject as MortgageCalculationResult;

In the next lesson, you create the main handler, which uses the others.