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 = {},
//dfd = $.Deferred(),
$element = Ox.Element(),
pending = {},
requests = {},
self = {
@ -59,6 +60,10 @@ Ox.Request = (function() {
});
}
},
/*@
bindEvent <f> bindEvent to error/authrize requests
@*/
bindEvent: $element.bindEvent,
// fixme: remove
_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) {
var $dialog, data, error;
var $dialog, data;
try {
data = JSON.parse(request.responseText);
} catch (err) {
@ -205,92 +179,8 @@ Ox.Request = (function() {
time: Ox.getTime()
};
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 {
// 0 (timeout) or 500 (error)
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);
$element.triggerEvent('error', data);
}
pending[options.id] = false;
}