I am using JQuery-UI dialog to load an MVC partial view
Here is some sample code:
$(function () {
function fSetupDialogs() {
$("#dialog-popup").dialog({
autoOpen: false,
resizable: true,
modal: true
});
}
});
$('body').on('click', '.popup-Edit',
function (e) {
url = $(this).data("url");
$("#dialog-popup")
.load(url)
.html("<img src='/content/images/Spinner.gif'>")
.dialog("option", "title", "Edit " + url.split("/").pop())
.dialog('open');
}
);
This works pretty well despite some occasional weird scrollbars and stuff.
The problem is when the view throws an error, I can't work out how to inspect it and display it inside the jqueryui panel
So if the view returns a 500 or 401 error, I'd like to capture that and show it in the dialog. Right now what happens is that the spinner GIF just sits there forever. If I open the F12 console I can see the error in there.
I have tried using the .error
event handler which does capture it and pop up a messagebox. But I want to display inside the popup:
// This pops up an error alert
$("#dialog-popup")
.load(url)
.error(alert("error"))
.html("<img src='/content/images/Spinner.gif'>")
.dialog("option", "title", "Edit " + url.split("/").pop())
.dialog('open');
How do I display something inside the dialog? This has no effect - the spinner stays in there
// This has no effect - I want to see the words "error"
$("#dialog-popup")
.load(url)
.error($("#dialog-popup").html("error"))
.html("<img src='/content/images/Spinner.gif'>")
.dialog("option", "title", "Edit " + url.split("/").pop())
.dialog('open');
For bonus points, how do I use the javascript error object to identify 401, 500 whatever? - I haven't got to that yet.
I looked through my own code and found the answer.
The load
method takes a callback argument that can be used to do what I need
I wrote a function called popupComplete
that is called when the load is finished.
// This loads a partial MVC view into the popup
$("#dialog-popup")
.load(url,popupComplete)
.html("<div align='middle'><img src='/content/images/Spinner.gif'></div>")
.dialog("option", "title", "New")
.dialog("open");
// when the load is complete, this is called
// which optionally displays an error inside the popup
function popupComplete(response, status, xhr) {
if (status === "error") {
var msg =
"<BR><BR><BR><H3 style='text-align: center;'><span style='color: red;' class='glyphicon glyphicon-fire'></span> Confound it!<BR><BR>" +
xhr.status + " " + xhr.statusText +
"<BR><BR>" + (xhr.status == 401 ? "Try logging in again" : "") +
"</B></H3>";
$("#dialog-popup").html(msg);
}
}