refactor request handling

This commit is contained in:
rolux 2013-07-28 01:07:42 +02:00
parent e7415059f0
commit f8da7fa1fb

View file

@ -129,9 +129,8 @@ Ox.Request = (function() {
options.age == -1
|| options.age > +new Date() - cache[req].time
)) {
var data = cache[req].data;
setTimeout(function() {
callback && callback(data);
callback(cache[req].data, true);
}, 0);
} else {
pending[options.id] = true;
@ -139,7 +138,7 @@ Ox.Request = (function() {
beforeSend: function(request) {
var csrftoken = Ox.Cookies('csrftoken');
if (csrftoken) {
request.setRequestHeader("X-CSRFToken", csrftoken);
request.setRequestHeader('X-CSRFToken', csrftoken);
}
},
complete: complete,
@ -155,14 +154,18 @@ Ox.Request = (function() {
});
}
function callback(data) {
function callback(data, success) {
if (requests[options.id]) {
delete requests[options.id];
options.callback && options.callback(data);
$element && $element.triggerEvent('request', {
requests: Ox.len(requests)
});
}
if (success) {
options.callback && options.callback(data);
} else {
$element && $element.triggerEvent('error', data);
}
}
function complete(request) {
@ -193,9 +196,9 @@ Ox.Request = (function() {
data: data,
time: Ox.getTime()
};
callback(data);
callback(data, true);
} else {
$element && $element.triggerEvent('error', data);
callback(data, false);
}
pending[options.id] = false;
}