I'm making some functions after opening the modal. RefreshBirths()
function needs motherId
and fatherId
which are from getDictionaryMother()
and getDictionaryFather()
functions (these render my mothers and father on page then I can get values in refreshBirths()
function).
I checked that refreshBirths()
takes my ids too early so a have undefined ids in the end.
I checked calling my refreshBirths()
function in .done
part in mother/father
methods and it fixes my problem but I prefer do it explicitly in the indicated part of the code.
This doesn't fix but I want to make this there:
$('#create-modal').on('shown.bs.modal', function() {
refreshBreeds();
getDictionaryMother();
getDictionaryFather();
$.when(getDictionaryFather()).done(function() {
refreshBirths();
});
});
Here it fixes but I prefer have that method called in another place
function getDictionaryMother() {
$.ajax({
url: "/admin/api/dictionary/" + "ANIMAL_MOTHER",
type: "GET",
dataType: "json"
})
.done(function(response) {
$('#mother-create').empty();
$('#mother').empty();
response.forEach(function(mother){
$('#mother-create').append('<option value='+ mother.id +'> '+ mother.value +' </option>');
$('#mother').append('<option value='+ mother.id +'> '+ mother.value +' </option>');
});
refreshBirths();
})
.fail(function(jqxhr, textStatus, errorThrown) {
displayErrorInformation("Cannot get dict: " + name + " due to: " + jqxhr.responseText);
});
}