oxjs/source/Ox.UI/js/Core/Ox.Request.js

261 lines
9.3 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
2011-05-16 08:24:46 +00:00
/*@
Ox.Request <o> Basic request handler object
FIXME: options is not a property, just documenting defaults
options <o> Options object
timeout <n|60000> request timeout
type <s|"POST"> request type, possible values POST, GET, PUT, DELETE
url <s> request url
2011-05-16 08:24:46 +00:00
@*/
2011-04-22 22:03:10 +00:00
2011-05-16 08:24:46 +00:00
Ox.Request = function(options) {
2011-04-22 22:03:10 +00:00
var cache = {},
//dfd = $.Deferred(),
pending = {},
requests = {},
self = {
options: Ox.extend({
2011-04-22 22:03:10 +00:00
timeout: 60000,
type: 'POST',
url: '/api/'
}, options)
};
return {
2011-05-16 08:24:46 +00:00
/*@
cancel <f> cancel pending requests
() -> <u> cancel all requests
(fn) -> <u> cancel all requests where function returns true
(id) -> <u> cancel request by id
2011-05-16 08:24:46 +00:00
@*/
2011-04-22 22:03:10 +00:00
cancel: function() {
if (arguments.length == 0) {
// cancel all requests
requests = {};
} else if (Ox.isFunction(arguments[0])) {
// cancel with function
Ox.forEach(requests, function(req, id) {
if (arguments[0](req)) {
delete requests[id];
}
});
} else {
// cancel by id
delete requests[arguments[0]];
}
},
2011-05-16 08:24:46 +00:00
/*@
clearCache <f> clear cached results
() -> <u> ...
2011-05-16 08:24:46 +00:00
@*/
2011-04-22 22:03:10 +00:00
clearCache: function() {
cache = {};
},
2011-08-06 17:41:01 +00:00
_leakCache: function() {
return cache;
},
2011-05-16 08:24:46 +00:00
/*@
options <f> get/set options
() -> <o> get options
(options) -> <o> set options
options <o> Options Object
@*/
2011-04-22 22:03:10 +00:00
options: function(options) {
return Ox.getset(self.options, options, $.noop(), this);
},
2011-05-16 08:24:46 +00:00
/*@
requests <f> pending requests
() -> <n> returns number of requests
@*/
2011-04-22 22:03:10 +00:00
requests: function() {
return Ox.len(requests);
},
2011-05-16 08:24:46 +00:00
/*@
send <f> send request
(options) -> <n> returns request id
options <o> Options Object
age <n|-1> cache age
id <n|Ox.uid()> request id
timeout <n|self.options.timeout> overwrite default timeout
type <n|self.options.timeout> overwrite default type
url <n|self.options.timeout> overwrite default url
@*/
2011-04-22 22:03:10 +00:00
send: function(options) {
var options = Ox.extend({
2011-04-22 22:03:10 +00:00
age: -1,
callback: null,
id: Ox.uid(),
timeout: self.options.timeout,
type: self.options.type,
url: self.options.url
}, options),
req = JSON.stringify({
url: options.url,
data: options.data
});
if (pending[options.id]) {
setTimeout(function() {
Ox.Request.send(options);
}, 0);
} else {
requests[options.id] = {
url: options.url,
data: options.data
};
if (cache[req] && (options.age == -1 || options.age > Ox.getTime() - cache[req].time)) {
setTimeout(function() {
callback && callback(cache[req].data);
}, 0);
} else {
pending[options.id] = true;
$.ajax({
data: options.data,
dataType: 'json',
error: error,
success: success,
timeout: options.timeout,
type: options.type,
url: options.url
});
}
}
function callback(data) {
delete requests[options.id];
//Ox.len(requests) == 0 && $body.trigger('requestStop');
options.callback && options.callback(data);
}
function debug(request) {
var $iframe = $('<iframe>')
.css({ // fixme: should go into a class
width: 768,
height: 384
}),
$dialog = Ox.Dialog({
2011-04-22 22:03:10 +00:00
title: 'Application Error',
buttons: [
Ox.Button({
2011-04-22 22:03:10 +00:00
title: 'Close'
})
.bindEvent({
click: function() {
$dialog.close();
}
})
],
content: $iframe,
2011-08-17 11:39:33 +00:00
width: 768,
height: 384
2011-04-22 22:03:10 +00:00
})
.open(),
iframe = $iframe[0].contentDocument || $iframe[0].contentWindow.document;
iframe.open();
iframe.write(request.responseText);
iframe.close();
}
function error(request, status, error) {
var data;
if (arguments.length == 1) {
data = arguments[0]
} else {
try {
data = JSON.parse(request.responseText);
} catch (err) {
try {
data = {
status: {
code: request.status,
text: request.statusText
}
};
} catch (err) {
data = {
status: {
code: '500',
text: 'Unknown Error'
}
};
}
}
}
if (data.status.code < 500) {
callback(data);
} else {
var $dialog = Ox.Dialog({
2011-04-22 22:03:10 +00:00
title: 'Application Error',
buttons: [
Ox.Button({
2011-04-22 22:03:10 +00:00
id: 'details',
title: 'Details'
})
.bindEvent({
click: function() {
$dialog.close(function() {
debug(request);
});
}
}),
Ox.Button({
2011-04-22 22:03:10 +00:00
id: 'close',
title: 'Close'
})
.bindEvent({
click: function() {
$dialog.close();
}
})
],
2011-08-17 11:39:33 +00:00
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, we have encountered an application error 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.')
),
height: 192,
2011-04-22 22:03:10 +00:00
keys: {enter: 'close', escape: 'close'},
2011-08-17 11:39:33 +00:00
width: 368
2011-04-22 22:03:10 +00:00
})
.open();
// fixme: change this to Send / Don't Send
/*Ox.print({
request: request,
status: status,
error: error
});*/
}
pending[options.id] = false;
}
function success(data) {
pending[options.id] = false;
cache[req] = {
data: data,
time: Ox.getTime()
};
callback(data);
}
// return dfd.promise();
return options.id;
}
};
}();