diff options
| author | Shyam Seshadri | 2010-06-23 13:07:31 -0700 |
|---|---|---|
| committer | Shyam Seshadri | 2010-06-23 13:07:31 -0700 |
| commit | 42257f22af643fc5e91162cc99adeef32916fd28 (patch) | |
| tree | 85f78cf493e79a44fc02d4749da681296d83dcea | |
| parent | 70c3dc81665191cd065a5303e5e26639a0023a73 (diff) | |
| download | angular.js-42257f22af643fc5e91162cc99adeef32916fd28.tar.bz2 | |
wilford's changes to serve cached data and then fetch from server if needed / specified
| -rw-r--r-- | src/Resource.js | 6 | ||||
| -rw-r--r-- | src/services.js | 9 | ||||
| -rw-r--r-- | test/servicesSpec.js | 16 |
3 files changed, 27 insertions, 4 deletions
diff --git a/src/Resource.js b/src/Resource.js index 724121b7..6ee0b1cf 100644 --- a/src/Resource.js +++ b/src/Resource.js @@ -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; } }; - diff --git a/src/services.js b/src/services.js index 5f42ef18..64f2ea4f 100644 --- a/src/services.js +++ b/src/services.js @@ -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); diff --git a/test/servicesSpec.js b/test/servicesSpec.js index c2c13461..f679a39b 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -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(){ |
