[4.2] - Multiple selectQuery callback loops

Posted by IramK on 10-Oct-2016 11:35

Hello,

I am using multiple rbf_selectQuery2() functions client-side and I would like to know what the best way to use this would be. I understand that select query callbacks are asynchronous but, is there a way I could perform multiple queries within callbacks?

rbf_selectQuery2("SELECT id from account where name like %google%", function accounts_callback(values) {
for(var i = 0; i < values.length; i++) {
rbf_selectQuery2("SELECT name, id, firstName, lastName FROM contact where accountId = " + values[i][0], function contact_callback(values2) {
for(var j =0; j < values2.length; j++) {
// get contact information
}
// After all the accounts are done and all the contacts are done, then I want this code to run
...
});
}
});

Best Regards,

Iram

Posted by Mohammed Siraj on 12-Oct-2016 04:41

Yes, there was an issue with closure context access.

So the requirements is to collect all contact record data and feed it as kendo dataSource. In this case, we just need to construct an array of contact records and need not have a map to track account id's.

This array will serve as kendo datasource.

Have re-worked the sample above.

All Replies

Posted by Mohammed Siraj on 10-Oct-2016 13:05

Iram, you can consider using JavaScript Promise concept for asynchronous computation.

In the example below, will be using jQuery library Deferred & Promise objects to ensure cross browser support.


var deferredInstance = $.Deferred();
var allContacts = [
]; //array of all contacts records
rbf_selectQuery2('SELECT id from account where name like %google%', function accounts_callback(values) {
  for (var i = 0; i < values.length; i++) {
    rbf_selectQuery2('SELECT name, id, firstName, lastName FROM contact where accountId = ' + values[i][0], function contact_callback(values2) {
      for (var j = 0; j < values2.length; j++) {
        // get contact information
        var contactRecord = {
        };
        contactRecord.name = values2[j][0];
        contactRecord.id = values2[j][1];
        allContacts.push(contactRecord);
      }
    });
  } //for

  deferredInstance.resolve(allContacts); //or directly call processContacts function here...
});
deferredInstance.done(processContacts);
function processContacts() {
  // After all the accounts are done , then I want this code to run
  //process allContacts array... feed it as kendo data source.. 
}

Eventhough JavaScript closure concept will let you nest functions and provide access to outer function scope, avoid nesting greater than two levels and leverage Deferred/Promise objects for asynchronous computation.

Hope this helps.

Posted by IramK on 12-Oct-2016 03:06

Hello [mention:78c86023544844079dc6455a4a7a4d57:e9ed411860ed4f2ba0265705b8793d05] ,

Thanks for your answer. A couple of questions on that answer:

1) values[i][0] is not accessible under that second selectQuery loop.

2) I am trying to populate a kendo grid using that "allContacts" map, however, for some reason because it is not like an array, it fails to populate the grid. Any suggestions on how I can get that "allContacts" variable to act as the dataSource for a kendo grid?

Cheers.

Iram

Posted by Ruben Dröge on 12-Oct-2016 03:16

I would check the structure of my data as is and put/parse it into a decent array and use that as the kendo datasource. Bit like you can see in the demo app in Marketplace where a Kendo organizational chart is rendered.

Posted by Mohammed Siraj on 12-Oct-2016 04:41

Yes, there was an issue with closure context access.

So the requirements is to collect all contact record data and feed it as kendo dataSource. In this case, we just need to construct an array of contact records and need not have a map to track account id's.

This array will serve as kendo datasource.

Have re-worked the sample above.

This thread is closed