I have now tried to read and understand about promises, asynchronous ajax calls and .when and .then. However I am still experiencing some problems with old content being updated and inserted.
So please let me know if I am doing something wrong or if there is a better approach.
My code:
// Loading template
function getTemplate(template) {
return $.ajax({
dataType: "json",
url: "/externalBookingModule/jsonp_example.php?" + template + "&url=" + document.domain + "&callback=?",
success: function(result){
$("#template").html(result['template']);
console.log(result['template']);
}
});
}
// Loading JSON content
function loadContent(content, div) {
return $.ajax({
dataType: "json",
url: "/externalBookingModule/jsonp_example.php?" + content + "&url=" + document.domain + "&callback=?",
success: function(result){
var template = $('#template').html();
Mustache.parse(template); // optional, speeds up future uses
var rendered = Mustache.render(template, result);
$(div).html(rendered);
}
});
}
// buttons
$("#target").on("click", '.nextButton', function() {
$.when(
getTemplate("templateCustomerInfo", "#template"))
.then(
loadContent("test1", "#target")
);
});
$("#target").on("click", "#searchDatesButton", function() {
$.when(
getTemplate("templateDates", "#template"))
.then(
loadContent("test1", "#searchContent")
);
});
Thank you very much!