Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0cec9eb

Browse files
feat(): accept cache
can get response in cache when cache is provided and response present in cache. This is useful when a resource does not change often (like a list of countries)
1 parent c0cb9f8 commit 0cec9eb

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/ngResource/resource.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
4040
* default set of resource actions. The declaration should be created in the following format:
4141
*
42-
* {action1: {method:?, params:?, isArray:?, headers:?},
43-
* action2: {method:?, params:?, isArray:?, headers:?},
42+
* {action1: {method:?, params:?, isArray:?, headers:?, cache:?},
43+
* action2: {method:?, params:?, isArray:?, headers:?, cache:?},
4444
* ...}
4545
*
4646
* Where:
@@ -55,6 +55,7 @@
5555
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
5656
* `returns` section.
5757
* - `headers` – {Object=} – Optional HTTP headers to send
58+
* - `cache` – {Object=} - Optional HTTP cache (see cache option for $http)
5859
*
5960
* @returns {Object} A resource "class" object with methods for the default set of resource actions
6061
* optionally extended with custom `actions`. The default set contains these actions:
@@ -136,7 +137,7 @@
136137
* The object returned from this function execution is a resource "class" which has "static" method
137138
* for each action in the definition.
138139
*
139-
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`.
140+
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params`, `headers` and `cache`.
140141
* When the data is returned from the server then the object is an instance of the resource type and
141142
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
142143
* operations (create, read, update, delete) on server-side data.
@@ -378,7 +379,8 @@ angular.module('ngResource', ['ng']).
378379
method: action.method,
379380
url: route.url(extend({}, extractParams(data, action.params || {}), params)),
380381
data: data,
381-
headers: extend({}, action.headers || {})
382+
headers: extend({}, action.headers || {}),
383+
cache: (action.cache) ? action.cache : false
382384
}).then(function(response) {
383385
var data = response.data;
384386

test/ngResource/resourceSpec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,45 @@ describe("resource", function() {
393393
$httpBackend.flush();
394394
expect(person.name).toEqual('misko');
395395
});
396+
397+
398+
describe('cache', function() {
399+
var cache, callback;
400+
401+
beforeEach(function() {
402+
callback = jasmine.createSpy('done');
403+
});
404+
405+
beforeEach(inject(function($cacheFactory) {
406+
cache = $cacheFactory('testCache');
407+
}));
408+
409+
function doFirstCacheRequest() {
410+
var Person = $resource('/Person/:id', {}, {
411+
get: {method: 'GET', cache: cache}
412+
});
413+
414+
$httpBackend.expect('GET', '/Person/123').respond('\n{\n"name":\n"misko"\n}\n');
415+
var person = Person.get({id:123}, callback);
416+
$httpBackend.flush();
417+
expect(person.name).toEqual('misko');
418+
}
419+
420+
it('should cache GET request when cache is provided', inject(function($rootScope) {
421+
doFirstCacheRequest();
422+
423+
$httpBackend.expect('GET', '/Person/123').respond('\n{\n"name":\n"misko"\n}\n');
424+
425+
var Person = $resource('/Person/:id', {}, {
426+
query: {method: 'GET', cache: cache}
427+
});
428+
var person = Person.get({id:123}, callback);
429+
$rootScope.$digest();
430+
431+
expect(callback).toHaveBeenCalledOnce();
432+
expect(callback.mostRecentCall.args[0].name).toBe('misko');
433+
}));
434+
});
396435

397436

398437
describe('failure mode', function() {

0 commit comments

Comments
 (0)