Using JSDOs to create mobile and web clients : Accessing standard CRUD and Submit operations : Update operation example
  

Update operation example

To modify an existing record in JSDO memory on the client, you can call the assign( ) method to assign values to one or more fields of either a working record or a specific record in JSDO memory, or you can assign a value directly to the field of a working record. Using the assign( ) method, you set the values of fields to be updated in an object that you pass as a parameter to the method.
You can call the assign( ) method on:
*A table reference on the JSDO that has a working record, as in the example below
*A specific progress.data.JSRecord object reference
When the assign( ) method completes, any working record settings that existed prior to calling the method remain unchanged.
You can also assign a value directly to a single field of a JSDO working record as follows:
Syntax
jsdo-ref.table-ref.field-ref = value;
jsdo-ref.field-ref = value;
Where:
jsdo-ref
The reference to a JSDO, and if the JSDO contains only a single table, an implied reference to any working record that is set for that table.
table-ref
A table reference with the name of a table in jsdo-ref memory that has a working record.
field-ref
A field reference on a table-ref with the name and value of a field in the working record of the referenced table.
value
The value to set the field referenced by field-ref.
After either a successful call to the assign( ) method or a successful assignment to a field-ref of a working record, the JSDO marks the affected record for pending update on the server.
Caution: Never write directly to a field-ref that you reference on the data property of a table-ref; use field-ref on the data property only to read the referenced field value. Writing field values using the data property does not mark the record for pending update on the server, nor does it initiate a re-sort the record in JSDO memory according to any order you have established using the autoSort property. For more information, see the description of the data property. To mark a record for pending update and automatically re-sort the record according to the autoSort property, you must assign a record field value using one of the mechanisms described above.
To synchronize the server database with existing records you have updated in JSDO memory without using a Submit operation, you call saveChanges( ) by passing either an empty parameter list to the method or a single parameter value of false. This executes the OpenEdge ABL or Rollbase server routine that implements the Data Object Update operation once for each updated record in JSDO memory. In addition, for any other changed records in JSDO memory, this call also executes the server routine that implements the associated Create or Delete operation once for each associated record change.
Note: When multiple pending record changes exist in JSDO memory, the saveChanges( ) method invoked without Submit groups invocation of the associated resource operations in the order of 1) all Delete operations, 2) all Create operations, and 3) all Update operations, and invokes each such operation one record at a time over the network.
When you call the saveChanges( ) method, if jQuery Promises are supported in your environment, it returns a Promise object on which you can register callbacks to handle completion of all record-change operations that it has invoked. Otherwise, you can subscribe event callbacks to handle the results of these operations.
Operation results are thus returned as follows:
1. For each Update operation that completes, the JSDO fires an afterUpdate event to execute any callbacks you have subscribed to handle that event. The JSDO also fires any afterCreate and afterDelete events to execute any callbacks you have subscribed to handle these events.
2. After all record-change operations complete, the JSDO fires an afterSaveChanges event to execute any callbacks you have subscribed to handle that event. In addition, any returned Promise object executes the Promise callbacks that you have registered, depending on the combined operation results. (Note that the signatures of all Promise callbacks are identical to the signature of the afterSaveChanges event callback.)
After the saveChanges( ) method and all resource operations that it has invoked successfully complete, no working records are set for the tables in the JSDO.
Note: If successful execution of a resource Create or Update operation results in changes to the record on the server that was sent from the client (for example, an update to a sequence value), JSDO memory is automatically synchronized with these server changes when the request object with these results is returned to the client.
Note: If an error occurs on the server, and the autoApplyChanges property has the default value of true, any updated record has its changes automatically backed out from JSDO memory on the client.
For more information on handling saveChanges( ) method results for individual record-change operations, see the description of the saveChanges( ) method.
Following is an example showing an OpenEdge ABL implementation of the Update operation and its invocation on a JSDO.
Note: The bold code in the JavaScript examples primarily trace the path of referenced JSDO method and callback parameters, as well as key code elements in the example, including those elements that are directly or indirectly referenced in the example description and notes.
* An OpenEdge ABL implementation for Update
* Client JavaScript code: Update