Skip to content

Commit 72d09ec

Browse files
committed
ajax_modal, handle network failure
1 parent 78abad5 commit 72d09ec

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

app/assets/javascripts/blacklight/ajax_modal.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,44 @@ Blacklight.ajaxModal.containerSelector = "[data-ajax-modal~=container]";
102102

103103
Blacklight.ajaxModal.modalCloseSelector = "[data-ajax-modal~=close], span.ajax-close-modal";
104104

105-
Blacklight.ajaxModal.receiveAjax = function (data) {
106-
var contents = data.responseText;
107-
108-
// does it have a data- selector for container?
109-
// important we don't execute script tags, we shouldn't.
110-
// code modelled off of JQuery ajax.load. https://github.com/jquery/jquery/blob/master/src/ajax/load.js?source=c#L62
111-
var container = $("<div>").
112-
append( jQuery.parseHTML(contents) ).find( Blacklight.ajaxModal.containerSelector ).first();
113-
if (container.size() !== 0) {
114-
contents = container.html();
115-
}
116-
117-
$(Blacklight.ajaxModal.modalSelector).find('.modal-content').html(contents);
118-
119-
// send custom event with the modal dialog div as the target
120-
var e = $.Event('loaded.blacklight.ajax-modal')
121-
$(Blacklight.ajaxModal.modalSelector).trigger(e);
122-
// if they did preventDefault, don't show the dialog
123-
if (e.isDefaultPrevented()) return;
105+
// Called on fatal failure of ajax load, function returns content
106+
// to show to user in modal. Right now called only for extreme
107+
// network errors.
108+
Blacklight.ajaxModal.onFailure = function(data) {
109+
var contents = "<div class='modal-header'>" +
110+
"<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>" +
111+
"Network Error</div>";
112+
$(Blacklight.ajaxModal.modalSelector).find('.modal-content').html(contents);
113+
$(Blacklight.ajaxModal.modalSelector).modal('show');
114+
}
124115

125-
$(Blacklight.ajaxModal.modalSelector).modal('show');
116+
Blacklight.ajaxModal.receiveAjax = function (data) {
117+
if (data.readyState == 0) {
118+
// Network error, could not contact server.
119+
Blacklight.ajaxModal.onFailure(data)
120+
}
121+
else {
122+
var contents = data.responseText;
123+
124+
// does it have a data- selector for container?
125+
// important we don't execute script tags, we shouldn't.
126+
// code modelled off of JQuery ajax.load. https://github.com/jquery/jquery/blob/master/src/ajax/load.js?source=c#L62
127+
var container = $("<div>").
128+
append( jQuery.parseHTML(contents) ).find( Blacklight.ajaxModal.containerSelector ).first();
129+
if (container.size() !== 0) {
130+
contents = container.html();
131+
}
132+
133+
$(Blacklight.ajaxModal.modalSelector).find('.modal-content').html(contents);
134+
135+
// send custom event with the modal dialog div as the target
136+
var e = $.Event('loaded.blacklight.ajax-modal')
137+
$(Blacklight.ajaxModal.modalSelector).trigger(e);
138+
// if they did preventDefault, don't show the dialog
139+
if (e.isDefaultPrevented()) return;
140+
141+
$(Blacklight.ajaxModal.modalSelector).modal('show');
142+
}
126143
};
127144

128145

0 commit comments

Comments
 (0)