Ox.Request: add cache option

This commit is contained in:
rlx 2013-08-27 14:26:52 +00:00
parent e93b196e67
commit a0dc7500ca

View file

@ -16,6 +16,7 @@ Ox.Request = (function() {
requests = {},
self = {
options: {
cache: true,
timeout: 60000,
type: 'POST',
url: '/api/'
@ -24,6 +25,17 @@ Ox.Request = (function() {
$element;
return {
/*@
bindEvent <f> Bind event
@*/
bindEvent: function() {
if (!$element) {
$element = Ox.Element();
}
$element.bindEvent.apply(this, arguments);
},
/*@
cancel <f> cancel pending requests
() -> <u> cancel all requests
@ -49,6 +61,7 @@ Ox.Request = (function() {
requests: Ox.len(requests)
});
},
/*@
clearCache <f> clear cached results
() -> <u> ...
@ -63,16 +76,6 @@ Ox.Request = (function() {
}
},
/*@
bindEvent <f> Bind event
@*/
bindEvent: function() {
if (!$element) {
$element = Ox.Element();
}
$element.bindEvent.apply(this, arguments);
},
/*@
options <f> get/set options
() -> <o> get options
@ -131,7 +134,7 @@ Ox.Request = (function() {
)) {
setTimeout(function() {
callback(cache[req].data, true);
}, 0);
});
} else {
pending[options.id] = true;
$.ajax({
@ -192,10 +195,12 @@ Ox.Request = (function() {
if (Ox.contains([200, 404, 409], data.status.code)) {
// we have to include not found and conflict
// so that handlers can handle these cases
cache[req] = {
data: data,
time: Ox.getTime()
};
if (self.options.cache) {
cache[req] = {
data: data,
time: Ox.getTime()
};
}
callback(data, true);
} else {
callback(data, false);