Skip to main contentMAF Configuration Practices

Replace existing method

About this task

When a controller is bound to the application, page, or datasource, the controller class can handle any events that are dispatched. If the controller has a function that matches the event name, it will be invoked to handle the event. For example, the Technician app has a sliding drawer (a dialog) which uses the “changeStatusHandler” controller. ChangeStatusHandler has a “changeStatus()” function that will be called when the “changeStatus” event is triggered by a button on the page. If there is a need to do some processing before or after the call to changeStatus(), the Configuration Application can add an AppCustomizations.js file where the behavior can be modified through javascript. The code works by swapping the controller’s implementation of changeStatus() with one of our own design. When the sliding drawer is first created, the dialog-initialized lifecycle event is sent and we can use that to replace the current changeStatus() call with another function. The dialogInitialized() function checks to see whether the dialog being initialized is “woStatusChangeDialog”, and if so it will get the dialog’s controller, save off the changeStatus() function and then set changeStatus to a new function. The new code calls the original changeStatus() function, but can also do some processing before and after.

Procedure

Step 1: Capture the method reference during initialization of the component.

let controller = obj.controllers[0];
let oldChangeStatus = controller.changeStatus.bind(c);

Step 2: Reassign the method reference to a new function.

controller.changeStatus = (evt) => {}

Step 3: AppCustomizations.js

// Custom Application Logic
class AppCustomizations{
applicationInitialized(app){
this.app = app;
}
dialogInitialized(obj) {
if(obj.name === 'woStatusChangeDialog'){