Intro
It can be done easily, jQuery
even provides method that will help you done this in a heartbeat. Basically all you need is to chain several ajax calls and process then with $.when
when they are done.
Example
Working example: http://jsfiddle.net/Gajotres/qNJS8/
In this example you can see 3 distinct calls using $.ajax
function. Also a warning, because jsFiddle
don't support ajax calls fetching xml all data retrieved are dummy data, you will only need to replace it wit a proper URL
.
Javascript:
$(document).on('pagebeforeshow', '[data-role="page"]',function(e,data){
// Multiple Ajax Requests
$.when(
parseXML({xml: "<cars><car><name>TOYOTA</name><country>JAPAN</country><pic>http://1call.ms/Websites/schwartz2012/images/toyota-rav4.jpg</pic><description>Toyota has announced that it will recall a total of 778,000 units that may have been manufactured with improperly-tightened nuts on the rear suspension.</description></car></cars>"}) ,
parseXML({xml: "<cars><car><name>RENAULT</name><country>FRANCE</country><pic>http://cdn2.carsdata.net/thumb/182x105/pics/Renault/renault-sandero-16-stepway-01.jpg</pic><description>Renault Sandero 16 Stepway - this car is manufactured by renault. Renault Sandero 16 Stepway - Get car information and expert advice from CarsData.</description></car></cars>"}) ,
parseXML({xml: "<cars><car><name>AUDI</name><country>GERMANY</country><pic>http://img2.netcarshow.com/Audi-RS6_Avant_2014_800x600_wallpaper_06.jpg</pic><description>The new Audi RS6 Avant unites 412 kW (560 hp) of power and 700 Nm (516.29 lb-ft) of torque with unrestricted practicality for everyday use and leisure.</description></car></cars>"})
).done(
function(data1, data2, data3)
{
var allData = [].concat(data1).concat(data2).concat(data3);
$(allData).find("car").each(function(){
$('#cars-data').append('<li><a href="#cars"><img src="' + $(this).find('pic').text() + '" title="sample" height="100%" width="100%"/><h3>Car type:<span> '+ $(this).find('name').text() +'</span></h3><p>' + $(this).find('description').text() + '</p></a></li>');
})
$('#cars-data').listview('refresh');
}
).fail(
function()
{
alert('Fail');
}
);
function parseXML(data)
{
return $.ajax({
type: "POST",
url: "/echo/xml/",
dataType: "xml",
data: data,
success: function(data) {}
});
}
});
Explanation
Basically to understand this you only need to understand method $.when
. It accepts multiple functions a parameters and provide done
and fail
callback. If we are reading same kind of data we can use concat
function to merge everything together. After that do whatever you want with that data.