[4.2] - Multiple selectQuery callback loops - Forum - Rollbase - Progress Community

[4.2] - Multiple selectQuery callback loops

 Forum

[4.2] - Multiple selectQuery callback loops

This question is answered

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

Verified Answer
  • 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.

    Regards,
    Siraj.
All Replies
  • 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.

    Regards,
    Siraj.
  • Hello ,

    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

  • 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.

  • 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.

    Regards,
    Siraj.