/* subscribe to event */
dsCustomer.ttCustomer.subscribe('afterUpdate', onAfterUpdate); /* some code that adds a record to JSDO memory */ var jsrecord = dsCustomer.ttCustomer.add( {State : 'MA'} ); /* some code that updates a record in JSDO memory */ jsrecord = dsCustomer.ttCustomer.find(function(jsrecord) { return (jsrecord.data.Name === 'Lift Tours'); }); if (jsrecord) {jsrecord.assign( {State: 'VT'} );} /* some code that deletes a record from JSDO memory */ jsrecord = dsCustomer.ttCustomer.find(function(jsrecord) { return (jsrecord.data.Name === 'Burrows Sport Shop'); }); if (jsrecord) {jsrecord.remove();} /* some other JSDO memory record changes . . . */ dsCustomer.autoApplyChanges = false; dsCustomer.saveChanges(true).done( /* Successful Submit operation */ function( jsdo, success, request ) { /* all record changes processed by the Submit succeeded */ /* for example, track the average customer balance in certain states */ var avgBalance = { MA : 0.0, maCount : 0, VT : 0.0, vtCount : 0 }; jsdo.ttCustomer.foreach( function(jsrecord) { if (jsrecord.data.State === 'MA') { /* sum balances for customers in MA . . . */ avgBalance.MA += jsrecord.data.Balance; avgBalance.maCount += 1; } if (jsrecord.data.State === 'VT') { /* sum balances for customers in VT . . . */ avgBalance.VT += jsrecord.data.Balance; avgBalance.vtCount += 1; } }); /* compute averages and process further . . . */ avgBalances.MA = avgBalances.MA / avgBalances.maCount; avgBalances.VT = avgBalances.VT / avgBalances.vtCount; jsdo.acceptChanges(); /* Accept all record changes */ }).fail( /* Unsuccessful Submit operation */ function( jsdo, success, request ) { /* one or more record changes processed by the Submit failed */ var lenErrors, errors, errorType; /* handle Submit operation errors */ console.log("Operation: Submit"); errors = jsdo.ttCustomer.getErrors(); lenErrors = errors.length; for (var idxError=0; idxError < lenErrors; idxError++) { /* Each error */ console.log(JSON.stringify(errors[idxError])); } jsdo.rejectChanges(); /* Reject all record changes */ /* NOTE: Where an error occurred on the Submit invocation, thus preventing the back end from processing the changes, you might want to retry the Submit after the error conditions have been cleared. }); function onAfterUpdate (jsdo , record , success , request ) { /* check for errors on any record update in the Submit operation */ if (success) { /* do the normal thing for a successful record update. for example, process updated record according to its Balance value */ if (record.data.Balance > $100000.00) { /* process a high balance condition . . . */ } } else { /* all error messages handled by the Promise.fail() callback */ } }; |
/* subscribe to event */
dsCustomer.ttCustomer.subscribe('afterUpdate', onAfterUpdate); /* some code that adds a record to JSDO memory */ var jsrecord = dsCustomer.ttCustomer.add( {State : 'MA'} ); /* some code that updates a record in JSDO memory */ jsrecord = dsCustomer.ttCustomer.find(function(jsrecord) { return (jsrecord.data.Name === 'Lift Tours'); }); if (jsrecord) {jsrecord.assign( {State: 'VT'} );}; /* some code that deletes a record from JSDO memory */ jsrecord = dsCustomer.ttCustomer.find(function(jsrecord) { return (jsrecord.data.Name === 'Burrows Sport Shop'); }); if (jsrecord) {jsrecord.remove();}; /* some other JSDO memory record changes . . . */ dsCustomer.autoApplyChanges = false; dsCustomer.saveChanges(true).done( /* Successful Submit operation */ function( jsdo, success, request ) { /* all record changes processed by the Submit succeeded */ /* for example, track the average customer balance in certain states */ var avgBalance = { MA : 0.0, maCount : 0, VT : 0.0, vtCount : 0 }; jsdo.ttCustomer.foreach( function(jsrecord) { if (jsrecord.data.State === 'MA') { /* sum balances for customers in MA . . . */ avgBalance.MA += jsrecord.data.Balance; avgBalance.maCount += 1; } if (jsrecord.data.State === 'VT') { /* sum balances for customers in VT . . . */ avgBalance.VT += jsrecord.data.Balance; avgBalance.vtCount += 1; } }); /* compute averages and process further . . . */ avgBalances.MA = avgBalances.MA / avgBalances.maCount; avgBalances.VT = avgBalances.VT / avgBalances.vtCount; jsdo.acceptChanges(); /* Accept all record changes */ }).fail( /* Unsuccessful Submit operation */ function( jsdo, success, request ) { /* one or more record changes processed by the Submit failed */ var lenErrors, errors, errorType; /* handle Submit operation errors */ console.log("Operation: Submit"); errors = jsdo.ttCustomer.getErrors(); lenErrors = errors.length; for (var idxError=0; idxError < lenErrors; idxError++) { /* Each error */ 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) { 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 record with this ID */ } if (errors[idxError].responseText) { console.log("HTTP FULL TEXT: " + errors[idxError].responseText); } } else { /* unexpected errorType */ console.log("UNEXPECTED ERROR TYPE: " + errors[idxError].type); } } jsdo.rejectChanges(); /* Reject all record changes */ /* NOTE: Where an error occurred on the Submit invocation, thus preventing the back end from processing the changes, you might want to retry the Submit after the error conditions have been cleared. }); function onAfterUpdate (jsdo , record , success , request ) { /* check for errors on any record update in the Submit operation */ if (success) { /* do the normal thing for a successful record update. for example, process updated record according to its Balance value */ if (record.data.Balance > $100000.00) { /* process a high balance condition . . . */ } } else { /* all error messages handled by the Promise.fail() callback */ } }; |