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

Create operation example

To create a new record in JSDO memory on the client, you call the add( ) method on a JSDO table reference. The fields of the new record are initialized with the values specified in an object that you pass to the method. For any fields whose values you do not provide in this object, the method provides default values taken from the Data Object schema in the Data Service Catalog.
When each invocation of the add( ) method completes, the new record becomes the working record for the associated table and the JSDO marks this record as a new record for pending creation on the server. (Note that if the table has child tables, a working record is not set for these child tables.)
To synchronize the server database with new records you have created 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 Create operation once for each newly created 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 Delete or Update 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 Create operation that completes, the JSDO fires an afterCreate event to execute any callbacks you have subscribed to handle that event. The JSDO also fires any afterDelete and afterUpdate 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 newly created record is automatically deleted 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 Create 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 Create
* Client JavaScript code: Create