You can subscribe event handlers to JSDO events using either the subscribe( ) method on the JSDO or by setting appropriate properties in a JSDO constructor. If you use the subscribe() method after the JSDO is instantiated and its JSDO memory has been loaded with records, you can also subscribe to events for the Data Object Create, Update, and Delete (CUD) operations.

When you subscribe an event handler function to a JSDO event, the parameter list for the function must match the parameter list defined for the event. All JSDO event handlers receive a reference to the JSDO as its first parameter and a reference to a request object as its last parameter that contains event results. All handlers for after* events receive a boolean parameter that indicates the success of the Data Object operation or operations for afterSaveChanges without Submit. All handlers for events fired by Data Object CUD operations receive a JSRecord object parameter. This parameter represents the record object in JSDO memory that is created, updated, or deleted.

Regardless of how you subscribe event handlers to an event, you can remove an event subscription for event handlers using the unsubscribe() or unsubscribeAll() methods. If an event has no event handler subscribed and the event fires, it returns no results.

The following code sample illustrates how to use event subscriptions:

const progress = require("@progress/jsdo-core").progress;

// This will be called after fill() is called
function onAfterFillCustomers (jsdo, success, request) {
  if (!success) {
    console.log("Error: status: " + request.xhr.status);
  }
  else {
    // Make some changes and save them to trigger the 'AfterSaveChanges' event
    jsdo.add({EmpNum: 1, LastName: "TEST" });
    jsdo.saveChanges(true);
  }
}

// This will be called after saveChanges() is called in onAfterFillCustomers 
function onAfterSaveChanges (jsdo, success, request) {
  console.log("DEBUG: AfterSaveChanges: " + success + " errors: " + jsdo.getErrors());
}

// This assumes you have already set up a JSDOSession
let employee = new progress.data.JSDO({name: 'Employee'});
employee.subscribe('AfterFill', onAfterFillCustomers, this);
employee.subscribe('AfterSaveChanges', onAfterSaveChanges, this);
employee.fill();