Using JSDOs to create mobile and web clients : Accessing standard CRUD and Submit operations : Read operation example : Client JavaScript code: Read
  
Client JavaScript code: Read
The following examples illustrate calling fill( ) on the dsCustomer JSDO with the single-table resource, ttCustomer, to load records from the server database into JSDO memory and using a returned Promise object to handle the results.
This example uses simple error handling to log the contents of each error object returned by the JSDO getErrors( ) method:
var strFilter = 'where CustNum < 100';
    
dsCustomer.fill(strFilter).done(
function( jsdo, success, request ) {
        /* for example, add code to display all records in a list */
        jsdo.foreach(function (jsrecord) {
            /* you can reference the fields as jsrecord.data.field . . . */
        });
}).fail(
function( jsdo, success, request ) {
var lenErrors,
errors,
errorType;

/* handle Read operation errors */
errors = jsdo.ttCustomer.getErrors();
lenErrors = errors.length;
for (var idxError=0; idxError < lenErrors; idxError++) { /* Each error */
console.log(JSON.stringify(errors[idxError]));
}
}
});
This example is identical to the previous one but with more complex error handling to log the contents of each error object using more readable output:
var strFilter = 'where CustNum < 100';
    
dsCustomer.fill(strFilter).done(
function( jsdo, success, request ) {
        /* for example, add code to display all records in a list */
        jsdo.foreach(function (jsrecord) {
            /* you can reference the fields as jsrecord.data.field . . . */
        });
}).fail(
function( jsdo, success, request ) {
var lenErrors,
errors,
errorType;

/* handle Read 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.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;
}
console.log("READ ERROR: " + errorType + errors[idxError].error);
if (errors[idxError].responseText) {
console.log("HTTP FULL TEXT: " + errors[idxError].responseText);
}
}
}
});
This sample code:
*Passes a filter parameter, strFilter, to the fill( ) method. This filter causes the method to load only those records with a CustNum value lower than 100.
*Calls done( ) on the returned Promise object to register a callback function that processes the returned records after successful invocation of the Read operation, and calls fail( ) on the returned Promise to register a callback function that processes the errors after an unsuccessful invocation of the Read operation.
Note: A reference to the same Promise object is returned by each Promise method called in a chain on the fill( ) method to register the callbacks. You can also save a reference to the Promise object returned by fill( ) and call each Promise method on it this reference in sequence.
The following example illustrates calling fill( ) on the dsCustomer JSDO to load records from the server database into JSDO memory and using the afterFill event to handle the results with the same complex error handling as in the previous example:
var strFilter = 'where CustNum < 100';

/* subscribe to event */
dsCustomer.subscribe('afterFill', onAfterFill);
  
dsCustomer.fill(strFilter);
    
function onAfterFill(jsdo , success , request ) {
var lenErrors,
errors,
errorType;

if (success) {
        /* for example, add code to display all records in a list */
        jsdo.foreach(function (jsrecord) {
            /* the code here is executed for each record on the table.
             you can reference the fields as jsrecord.data.field . . . */
        });
    }
    else {
/* handle Read 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.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;
}
console.log("READ ERROR: " + errorType + errors[idxError].error);
if (errors[idxError].responseText) {
console.log("HTTP FULL TEXT: " + errors[idxError].responseText);
}
}
}
};
This sample code:
*Passes a filter parameter, strFilter, to the fill( ) method. This filter causes the method to load only those records with a CustNum value lower than 100.
*Subscribes a callback function, onAfterFill, to the afterFill event to enable processing of the returned records on successful invocation of the Read operation and handling of the returned errors on unsuccessful invocation of the Read operation.