EDIT: The base question is the same, but instead of a global buttons array getting passed back, I'm now passing back a getButtons() function.
I'm making an AJAX request to the server to get data to populate a popup box, and it's passing the data back as a JSP. Part of the JSP includes a JSON object with data used by the client to draw some buttons.
The issue is that sometimes the JSON object isn't ready on the success callback, so the buttons don't get drawn. The rest of the JSP is there, so the ancillary functionality works fine.
For most pages this does work flawlessly, but on certain pages, where the content is ready much more quickly, the buttons haven't yet been set. How can I ensure that buttons will be set?
Server code:
//Arbitrary JSP code prior to this function
function getButtons() {
var buttons = [];
buttons.push(addButton('Send', 'functionName', buttonDescription));
buttons.push(addButton('Cancel', 'functionName', buttonDescription));
return buttons;
}
Client code:
popupBoxFunction(action, params, global) {
$.ajax({
url: action,
type: 'POST',
data: params,
dataType: 'html',
global: global,
success: function (data, textStatus, xhr) {
$("#container").html(data);
if (typeof getButtons() != "undefined") {
var myButtons = generatePopupButton(getButtons());
for (var i = 0; i < myButtons.length; i++) {
//do something
}
}
//arbitrary rest of function
}