How to use the LABKEY.MultiRequest Class?

LabKey Support Forum (Inactive)
How to use the LABKEY.MultiRequest Class? slangley  2011-11-29 15:34
Status: Closed
 
If I put the attached code into a Wiki WebPart, I would expect that the LABKEY.MultiRequest class will execute the three requests I have added to it, and then call the final callback method when the three requests complete.

But the final callback method seems to never get called. What am I doing wrong?

Thanks.
 
 
Ben Bimber responded:  2011-11-29 16:19
hi scott,

i think you want to follow a pattern more along these lines:


var result1;
var result2;
var assays;

var multi = new LABKEY.MultiRequest();

multi.add(LABKEY.Query.selectRows, {
  schemaName: 'study',
  queryName: 'query',
  scope: this,
  success: function(results){result1 = results);}
});

multi.add(LABKEY.Query.selectRows, {
  schemaName: 'study',
  queryName: 'query2',
  scope: this,
  success: function(results){result2 = results;}
});

multi.add(LABKEY.assay.getAll, {
  success: function(results){assays = results;}
});

multi.send(onSuccess, this);

function onSuccess(){
  //your code here
console.log(result1);
console.log(result2);
console.log(assays);
}

in the example you posted, you wrapped the calls to selectRows() in a function and passed these functions to MultiRequest. Rather than actually call selectRows, you want to pass the asyncronous function to MultiRequest and let it call it. If you look at how MultiRequest works, you'll see it's just using the success callback from each AJAX call (multirequest.js ~line 140). When selectRows fires the success callback is called, MultiRequest knows it was completed. in theory you could pass this to multiRequest and have it work:

function sillyFunction(config){
  if(config.success)
     config.success.defer(100, this);
}
var multi = new LABKEY.MultiRequest();

multi.add(sillyFunction, {success: function(){console.log('success!')}});

multi.send(function(){
    console.log('more success!')
}, this);

note that all sillyFunction does is to fire a success callback, but that's sufficient for MultiRequest to decide it completed and fire it's own success function.