1

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);
    });
}
Gass
  • 7,536
  • 3
  • 37
  • 41
HappyProgrammer
  • 139
  • 1
  • 10
  • You could add a callback function as parameter and then in the method call it in the end i.e. after handling the response from the ajax request. – maraca Sep 27 '21 at 11:02

1 Answers1

0

You can use .then() method on the Ajax request or .when().

Ayush Oli
  • 49
  • 2
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '21 at 13:50