mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
wilford's changes to serve cached data and then fetch from server if needed / specified
This commit is contained in:
parent
70c3dc8166
commit
42257f22af
3 changed files with 27 additions and 4 deletions
|
|
@ -94,6 +94,8 @@ ResourceFactory.prototype = {
|
|||
function(status, response) {
|
||||
if (status == 200) {
|
||||
if (action.isArray) {
|
||||
if (action.cacheThenRetrieve)
|
||||
value = [];
|
||||
foreach(response, function(item){
|
||||
value.push(new Resource(item));
|
||||
});
|
||||
|
|
@ -104,7 +106,8 @@ ResourceFactory.prototype = {
|
|||
} else {
|
||||
throw {status: status, response:response, message: status + ": " + response};
|
||||
}
|
||||
}
|
||||
},
|
||||
action.cacheThenRetrieve
|
||||
);
|
||||
return value;
|
||||
};
|
||||
|
|
@ -135,4 +138,3 @@ ResourceFactory.prototype = {
|
|||
return Resource;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -313,7 +313,7 @@ angularService('$xhr.bulk', function($xhr, $error, $log){
|
|||
|
||||
angularService('$xhr.cache', function($xhr){
|
||||
var inflight = {}, self = this;;
|
||||
function cache(method, url, post, callback){
|
||||
function cache(method, url, post, callback, cacheThenRetrieve){
|
||||
if (isFunction(post)) {
|
||||
callback = post;
|
||||
post = null;
|
||||
|
|
@ -322,7 +322,11 @@ angularService('$xhr.cache', function($xhr){
|
|||
var data;
|
||||
if (data = cache.data[url]) {
|
||||
callback(200, copy(data.value));
|
||||
} else if (data = inflight[url]) {
|
||||
if (!cacheThenRetrieve)
|
||||
return;
|
||||
}
|
||||
|
||||
if (data = inflight[url]) {
|
||||
data.callbacks.push(callback);
|
||||
} else {
|
||||
inflight[url] = {callbacks: [callback]};
|
||||
|
|
@ -340,6 +344,7 @@ angularService('$xhr.cache', function($xhr){
|
|||
});
|
||||
});
|
||||
}
|
||||
|
||||
} else {
|
||||
cache.data = {};
|
||||
cache.delegate(method, url, post, callback);
|
||||
|
|
|
|||
|
|
@ -306,12 +306,28 @@ describe("service", function(){
|
|||
cache('GET', '/url', null, callback);
|
||||
xhr.flush();
|
||||
expect(log).toEqual('"first";"first";');
|
||||
cache('GET', '/url', null, callback, false);
|
||||
xhr.flush();
|
||||
expect(log).toEqual('"first";"first";"first";');
|
||||
});
|
||||
|
||||
it('should first return cache request, then return server request', function(){
|
||||
xhr.expectGET('/url').respond('first');
|
||||
cache('GET', '/url', null, callback, true);
|
||||
xhr.flush();
|
||||
xhr.expectGET('/url').respond('ERROR');
|
||||
cache('GET', '/url', null, callback, true);
|
||||
expect(log).toEqual('"first";"first";');
|
||||
xhr.flush();
|
||||
expect(log).toEqual('"first";"first";"ERROR";');
|
||||
});
|
||||
|
||||
it('should serve requests from cache', function(){
|
||||
cache.data.url = {value:'123'};
|
||||
cache('GET', 'url', null, callback);
|
||||
expect(log).toEqual('"123";');
|
||||
cache('GET', 'url', null, callback, false);
|
||||
expect(log).toEqual('"123";"123";');
|
||||
});
|
||||
|
||||
it('should keep track of in flight requests and request only once', function(){
|
||||
|
|
|
|||
Loading…
Reference in a new issue