/* subscribe to event */
dsCustomer.ttCustomer.subscribe('afterCreate', onAfterCreate); /* some code that adds a record and sends it to the server */ var jsrecord = dsCustomer.ttCustomer.add( {State : 'MA'} ); /* some other JSDO memory record changes . . . */ dsCustomer.saveChanges(false).done( /* Successful method execution */ function( jsdo, success, request ) { /* all resource operations invoked by saveChanges() succeeded */ /* for example, process all records based on their State value */ jsdo.ttCustomer.foreach( function(jsrecord) { if (jsrecord.data.State === 'MA') { /* process all records for the state of MA . . . */ } if (jsrecord.data.State === 'VT') { /* process all records for the state of VT . . . */ } }); }).fail( /* Unsuccessful method execution */ function( jsdo, success, request ) { /* one or more resource operations invoked by saveChanges() failed */ var lenErrors, errors, errorType; /* handle all operation errors */ errors = jsdo.ttCustomer.getErrors(); lenErrors = errors.length; for (var idxError=0; idxError < lenErrors; idxError++) { /* Each error */ console.log(JSON.stringify(errors[idxError])); } }); function onAfterCreate (jsdo , record , success , request ) { /* check for errors on any Create operation */ if (success) { /* do the normal thing for a successful Create operation. for example, process new record according to its State value */ switch (record.data.State ) { case 'MA': /* process a record created for MA . . . */ break; case 'VT': /* processs a record created for VT . . . */ break; default: /* process a record created for any other state . . . */ break; } } else { /* all error messages handled by the Promise.fail() callback */ } }; |
/* subscribe to event */
dsCustomer.ttCustomer.subscribe('afterCreate', onAfterCreate); /* some code that adds a record and sends it to the server */ var jsrecord = dsCustomer.ttCustomer.add( {State : 'MA'} ); /* some other JSDO memory record changes . . . */ dsCustomer.saveChanges(false).done( /* Successful method execution */ function( jsdo, success, request ) { /* all resource operations invoked by saveChanges() succeeded */ /* for example, process all records based on their State value */ jsdo.ttCustomer.foreach( function(jsrecord) { if (jsrecord.data.State === 'MA') { /* process all records for the state of MA . . . */ } if (jsrecord.data.State === 'VT') { /* process all records for the state of VT . . . */ } }); }).fail( /* Unsuccessful method execution */ function( jsdo, success, request ) { /* one or more resource operations invoked by saveChanges() failed */ var lenErrors, errors, errorType; /* handle all operation errors */ 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) { /* 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 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); } } }); function onAfterCreate (jsdo , record , success , request ) { /* check for errors on any Create operation */ if (success) { /* do the normal thing for a successful Create operation. for example, process new record according to its State value */ switch (record.data.State ) { case 'MA': /* process a record created for MA . . . */ break; case 'VT': /* processs a record created for VT . . . */ break; default: /* process a record created for any other state . . . */ break; } } else { /* all error messages handled by the Promise.fail() callback */ } }; |