turn request errors into events

This commit is contained in:
j 2012-01-14 16:18:26 +05:30
parent 8384fcc913
commit 36c1cda12a

View file

@ -13,6 +13,7 @@ Ox.Request = (function() {
var cache = {}, var cache = {},
//dfd = $.Deferred(), //dfd = $.Deferred(),
$element = Ox.Element(),
pending = {}, pending = {},
requests = {}, requests = {},
self = { self = {
@ -59,6 +60,10 @@ Ox.Request = (function() {
}); });
} }
}, },
/*@
bindEvent <f> bindEvent to error/authrize requests
@*/
bindEvent: $element.bindEvent,
// fixme: remove // fixme: remove
_leakCache: function() { _leakCache: function() {
@ -145,39 +150,8 @@ Ox.Request = (function() {
} }
} }
/*
function debug(request) {
var $iframe = $('<iframe>')
.css({ // fixme: should go into a class
width: 768,
height: 384
}),
$dialog = Ox.Dialog({
title: 'Application Error',
buttons: [
Ox.Button({
title: 'Close'
})
.bindEvent({
click: function() {
$dialog.close();
}
})
],
content: $iframe,
width: 768,
height: 384
})
.open(),
iframe = $iframe[0].contentDocument || $iframe[0].contentWindow.document;
iframe.open();
iframe.write(request.responseText);
iframe.close();
}
*/
function complete(request) { function complete(request) {
var $dialog, data, error; var $dialog, data;
try { try {
data = JSON.parse(request.responseText); data = JSON.parse(request.responseText);
} catch (err) { } catch (err) {
@ -205,92 +179,8 @@ Ox.Request = (function() {
time: Ox.getTime() time: Ox.getTime()
}; };
callback(data); callback(data);
} else if (data.status.code == 401 || data.status.code == 403) {
// unauthorized or forbidden
$dialog = Ox.Dialog({
buttons: [
Ox.Button({
id: 'close',
title: 'Close'
})
.bindEvent({
click: function() {
$dialog.close();
}
})
],
content: Ox.Element()
.append(
$('<img>')
.attr({src: Ox.UI.PATH + 'png/icon128.png'})
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
)
.append(
Ox.Element()
.css({position: 'absolute', left: '96px', top: '16px', width: '256px'})
.html('Sorry, you have made an unauthorized request.')
),
fixedSize: true,
height: 192,
keys: {enter: 'close', escape: 'close'},
title: Ox.toTitleCase(data.status.text),
width: 368
})
.open();
} else { } else {
// 0 (timeout) or 500 (error) $element.triggerEvent('error', data);
error = data.status.code == 0 ? 'timeout' : 'error';
// on window unload, pending request will time out, so
// in order to keep the dialog from appearing, delay it
setTimeout(function() {
$dialog = Ox.Dialog({
buttons: [
/*
Ox.Button({
id: 'details',
title: 'Details'
})
.bindEvent({
click: function() {
$dialog.close(function() {
debug(request);
});
}
}),
*/
Ox.Button({
id: 'close',
title: 'Close'
})
.bindEvent({
click: function() {
$dialog.close();
}
})
],
content: Ox.Element()
.append(
$('<img>')
.attr({src: Ox.UI.PATH + 'png/icon128.png'})
.css({position: 'absolute', left: '16px', top: '16px', width: '64px', height: '64px'})
)
.append(
Ox.Element()
.css({position: 'absolute', left: '96px', top: '16px', width: '256px'})
.html(
'Sorry, a server ' + error
+ ' occured while handling your request. To help us find out what went wrong, you may want to report this error to an administrator. Otherwise, please try again later.'
)
),
fixedSize: true,
height: 192,
keys: {enter: 'close', escape: 'close'},
title: 'Server ' + Ox.toTitleCase(error),
width: 368
})
.open();
// fixme: change this to Send / Don't Send
}, error == 'timeout' ? 1000 : 0);
} }
pending[options.id] = false; pending[options.id] = false;
} }