How to iterate a read service dataset in javascript

Posted by riche on 19-Mar-2015 08:49

I am new to the mobile scene and would appreciate guidance from anyone willing.

I have a select, that is populated with customers. I want to display the selected customers information in a grid below it. I have all of the information, so I don't want to request the data over again.

So, on the value change event, I wrote the following code that does work:

if (CustomersReadService !== undefined && CustomersReadService.data !== undefined && CustomersReadService.data.dsCustomers !== undefined && CustomersReadService.data.dsCustomers.ttCustomers !== undefined && CustomersReadService.data.dsCustomers.ttCustomers.length > 0) {

var selectedValue = Progress("selectCustomers").val();
var customerCount = CustomersReadService.data.dsCustomers.ttCustomers.length;
for (var i=0; i < customerCount ; i++) {
if (CustomersReadService.data.dsCustomers.ttCustomersIdea.custNumber == selectedValue) {
var customer = CustomersReadService.data.dsCustomers.ttCustomersIdea;
Progress('lbAddress').text(customer.address);
Progress('lbPhone').text(customer.phone);
Progress('lbFax').text(customer.fax);
... 
i = customerCount ;
}
}
}

Is there a better way of getting the record that I want? I was going to use the jsdo.find method, but couldn't figure out how to. I couldn't figure out the foreach either, so I went with a standard array loop option. The find and foreach are undefined if I try them on any level of the service. I noticed that the service is actually an Appery.Datasource, but can't find any information about traversing them either.

All Replies

Posted by riche on 19-Mar-2015 09:54

OK. I figured my own question out. I didn't understand that the jsdo service stored the read service stuff. I also can't refer to it by the name given for that page, but the actual service name. Once I understood this, I tried the following, which worked.

var selectedValue = Progress("selectCustomers").val();

var custRecord = MobileService_beoCustomer_JSDO.jsdo.find(function(custRec) {

return (custRec.data.custNumber == selectedValue);

});

if (custRecord !== undefined) {

Progress('lbAddress').text(custRecord.data.address1);

Progress('lbPhone').text(custRecord.data.phone);

Progress('lbFax').text(custRecord.data.fax);

...

}

This thread is closed