My plan involves executing 2 AJAX calls:
- Loading a partial HTML template.
- Fetching JSON data for the template and displaying it within the loaded template.
The JSON must be retrieved separately from the template because users might initiate a "refresh" action. The template cannot be loaded upon the initial page load due to the presence of tab controls, where each tab needs to be loaded as needed.
Let's assume that a function called loadData
has been invoked. In this case, the following steps need to be taken:
- If the template is already loaded, proceed to step 3.
- Simultaneously send AJAX requests for both the template using
$().load
and the JSON data using$.getJSON
. It is possible to send these requests together without waiting for the template to finish loading, correct? - Once the JSON data has been fetched, check if the template is already present. If so, render the data into the template. Otherwise, wait for the template to finish loading before rendering the data upon successful retrieval.
I am curious about the best practice for handling such tasks. Is there a comprehensive solution available for this scenario?
Thank you in advance.