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