JSDO properties, methods, and events reference : afterSaveChanges event
  

afterSaveChanges event

Fires once for each call to the saveChanges( ) method on a JSDO, after responses to all create, update, and delete record requests sent to a Data Object resource have been received from the server.
This event fires after all responses have been received from one or more Create, Update, and Delete (CUD) operation requests (sent without using Submit), and fires after the one response has been received from a single Submit operation request after its one or more record changes have completed.
Note: A single CUD operation request is sent for each record object that you have created, updated, or deleted (respectively) in the JSDO with a single call to saveChanges( ) or saveChanges(false). A single Submit operation request for any and all created, updated, and deleted JSDO record objects is sent with a single call to saveChanges(true).
Applies to: progress.data.JSDO class
The following parameters appear in the signature of any event handler (callback) function (or functions) that you subscribe to this event:

Syntax

function ( jsdo , success , request )
jsdo
A reference to the JSDO that invoked the saveChanges( ) method for one or more record changes on its resource. For more information, see the description of the jsdo property of the request object.
success
A boolean that is true if all record changes invoked on the resource by saveChanges( ) were successful. For more information, see the description of the success property of the request object.
request
A reference to the request object returned after the saveChanges( ) method completed execution and returned all results from its record changes on the server. For more information, see the description of the request object.
Application code can subscribe a callback to this event by invoking the subscribe( ) method on a JSDO instance.

Example

The following code fragment subscribes the function, onAfterSaveChanges, to handle the afterSaveChanges event fired on the JSDO, myjsdo, for changes saved on an OpenEdge single-table resource, ttCustomer without using a Submit operation:
/* subscribe to event */
myjsdo.subscribe( 'afterSaveChanges', onAfterSaveChanges );
/* some code that initiates multiple CRUD operations and
   sends them to the server */
var newrec = myjsdo.add();
. . .

var jsrecord = myjsdo.findById(myid);
if (jsrecord) {jsrecord.remove();};

myjsdo.saveChanges(); /* invoked without Submit */

function onAfterSaveChanges( jsdo , success , request ) {
    /* number of resource operations invoked by saveChanges() */
    var lenErrors,
errors,
errorType;
    if (success) {
        /* all resource operations invoked by saveChanges() succeeded */
        /* for example, redisplay records in list */
        jsdo.foreach( function(jsrecord) {
            /* reference the record/field as jsrecord.data.<fieldName> */
        });
    }
    else {
/* handle all operation errors */
errors = jsdo.ttCustomer.getErrors();
lenErrors = errors.length;
for (var idxError=0; idxError < lenErrors; idxError++) {
switch(errors[idxError].type) {
case progress.data.JSDO.DATA_ERROR:
errorType = "Server Data Error: ";
break;
case progress.data.JSDO.RETVAL:
errorType = "Server App Return Value: ";
break;
case progress.data.JSDO.APP_ERROR:
errorType = "Server App Error #"
+ errors[idxError].errorNum + ": ";
break;
case progress.data.JSDO.ERROR:
errorType = "Server General Error: ";
break;
case default:
errorType = null; // Unexpected errorType value
break;
}
if (errorType) { /* log all error text
console.log("ERROR: " + errorType + errors[idxError].error);
if (errors[idxError].id) { /* error with record object */
console.log("RECORD ID: " + errors[idxError].id);
/* possibly log the data values for the record with this ID
from the matching request.batch.operations[index].jsrecord */
}
if (errors[idxError].responseText) {
console.log("HTTP FULL TEXT: "
+ errors[idxError].responseText);
}
}
else { /* unexpected errorType */
console.log("UNEXPECTED ERROR TYPE: "
+ errors[idxError].type);
}
}
    }
};
If successful, the example loops to display the data from every record in JSDO memory (not shown). If unsuccessful, the example loops through the array of error objects returned from the call to saveChanges( ) (the result of calling jsdo.ttCustomer.getErrors( )) and logs annotated values for each error object returned, depending on its type (errors[idxError].type). If a record ID is included for a given error object, you can display the field values for the associated record (not shown) that you can return from request.batch.operations[index].jsrecord, where index identifies a returned request object for an operation in the request.batch.operations array and the error record ID matches getId(request.batch.operations[index].jsrecord).
Note: This example takes advantage of the default setting of the JSDO autoApplyChanges property (true), which automatically accepts or rejects changes to JSDO memory based on whether all CUD operations were successful. Note also that for an unsuccessful result, the error information returned by getErrors( ) is still available until the next invocation of fill( ) or saveChanges( ).

See also:

beforeSaveChanges event, saveChanges( ) method, subscribe( ) method (JSDO class), unsubscribe( ) method (JSDO class)