I'm looping through a marker layer in a Leaflet map to catch the latitude and longitude of each marker and sending this coordinates in my database with Ajax.
Then, when I've sent the latitude and longitude of each marker in my database, I want to call my WFS layer with another Ajax to show the new markers added to my db.
For that, I want to call my second Ajax afther the first one has finished, so I tried to use $.when().done(), but it doesn't works.
If I'm sending the coordinates of a few markers, it works, but if I try sending 200 markers, the second Ajax is executed before the end of the first one, and my WFS layer is not displayed. If I set a timeout on se second Ajax to give time to the first Ajax to execute, it works, but it's not a solution.
Here is my code :
var dialog_create = $('#dialog_create').dialog();
dialog_create.dialog(options, {
buttons: {
Add: function() {
$.when(
layer.eachLayer(function(layer) {
latGPS = layer.getLatLng().lat;
lngGPS = layer.getLatLng().lng;
$('#latitudeEP').val(latGPS);
$('#longitudeEP').val(lngGPS);
data = $("#formulaireEP").serialize();
$.ajax({
url: 'assets/php/create/create_EP.php',
type: $("#formulaireEP").attr('method'),
data: data,
success: function() {
dialog_create_EP.dialog("close");
$("#formulaireEP")[0].reset();
}
})
})
).done(function(data) {
//setTimeout(function(){
$.ajax({
url: owsrootUrlAssainissement + L.Util.getParamString(parametersEP),
dataType: 'jsonp',
jsonpCallback: 'callEP'
}).done(EPvannes1);
//},5000);
});
return false;
},
Cancel: function() {
dialog_create_EP.dialog("close");
},
}
});
dialog_create_EP.dialog("open");
EDIT :
Here is my best attempt using a deffered object, but it doesn't works...
var dialog_create = $('#dialog_create').dialog();
dialog_create.dialog(options, {
buttons: {
Add: function() {
var defer = $.Deferred();
function getAjaxDeffered(){
layer.eachLayer(function(layer) {
latGPS = layer.getLatLng().lat;
lngGPS = layer.getLatLng().lng;
$('#latitudeEP').val(latGPS);
$('#longitudeEP').val(lngGPS);
data = $("#formulaireEP").serialize();
$.ajax({
url: 'assets/php/create/create_EP.php',
type: $("#formulaireEP").attr('method'),
data: data,
success: function() {
dialog_create_EP.dialog("close");
$("#formulaireEP")[0].reset();
}
})
})
}
defer.resolve(getAjaxDeffered());
$.when(defer).then(function(data) {
//setTimeout(function(){
$.ajax({
url: owsrootUrlAssainissement + L.Util.getParamString(parametersEP),
dataType: 'jsonp',
jsonpCallback: 'callEP'
}).done(EPvannes1);
//},5000);
});
return false;
},
Cancel: function() {
dialog_create_EP.dialog("close");
},
}
});
dialog_create_EP.dialog("open");