Skip to content

Commit 58cd576

Browse files
committed
Closes #35
1 parent f568bc2 commit 58cd576

File tree

7 files changed

+92
-28
lines changed

7 files changed

+92
-28
lines changed

dist/angular-data.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,8 @@ var errorPrefix = 'DS.find(resourceName, id[, options]): ';
18581858
* @param {string|number} id The primary key of the item to retrieve.
18591859
* @param {object=} options Optional configuration. Properties:
18601860
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
1861+
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
1862+
*
18611863
* @returns {Promise} Promise produced by the `$q` service.
18621864
*
18631865
* ## Resolves with:
@@ -1883,6 +1885,11 @@ function find(resourceName, id, options) {
18831885
} else if (!this.utils.isObject(options)) {
18841886
deferred.reject(new this.errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } }));
18851887
} else {
1888+
if (!('cacheResponse' in options)) {
1889+
options.cacheResponse = true;
1890+
} else {
1891+
options.cacheResponse = !!options.cacheResponse;
1892+
}
18861893
try {
18871894
var definition = this.definitions[resourceName],
18881895
resource = this.store[resourceName],
@@ -1896,10 +1903,14 @@ function find(resourceName, id, options) {
18961903
if (!(id in resource.pendingQueries)) {
18971904
promise = resource.pendingQueries[id] = _this.adapters[options.adapter || definition.defaultAdapter].find(definition, id, options)
18981905
.then(function (data) {
1899-
// Query is no longer pending
1900-
delete resource.pendingQueries[id];
1901-
resource.completedQueries[id] = new Date().getTime();
1902-
return _this.inject(resourceName, data);
1906+
if (options.cacheResponse) {
1907+
// Query is no longer pending
1908+
delete resource.pendingQueries[id];
1909+
resource.completedQueries[id] = new Date().getTime();
1910+
return _this.inject(resourceName, data);
1911+
} else {
1912+
return data;
1913+
}
19031914
});
19041915
}
19051916

@@ -1960,10 +1971,14 @@ function _findAll(utils, resourceName, params, options) {
19601971
// This particular query has never even been made
19611972
resource.pendingQueries[queryHash] = _this.adapters[options.adapter || definition.defaultAdapter].findAll(definition, { params: params }, options)
19621973
.then(function (data) {
1963-
try {
1964-
return processResults.apply(_this, [utils, data, resourceName, queryHash]);
1965-
} catch (err) {
1966-
throw new _this.errors.UnhandledError(err);
1974+
if (options.cacheResponse) {
1975+
try {
1976+
return processResults.apply(_this, [utils, data, resourceName, queryHash]);
1977+
} catch (err) {
1978+
throw new _this.errors.UnhandledError(err);
1979+
}
1980+
} else {
1981+
return data;
19671982
}
19681983
});
19691984
}
@@ -2026,6 +2041,7 @@ function _findAll(utils, resourceName, params, options) {
20262041
*
20272042
* @param {object=} options Optional configuration. Properties:
20282043
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
2044+
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
20292045
*
20302046
* @returns {Promise} Promise produced by the `$q` service.
20312047
*
@@ -2053,6 +2069,11 @@ function findAll(resourceName, params, options) {
20532069
} else if (!this.utils.isObject(options)) {
20542070
deferred.reject(new this.errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } }));
20552071
} else {
2072+
if (!('cacheResponse' in options)) {
2073+
options.cacheResponse = true;
2074+
} else {
2075+
options.cacheResponse = !!options.cacheResponse;
2076+
}
20562077
try {
20572078
promise = promise.then(function () {
20582079
return _findAll.apply(_this, [_this.utils, resourceName, params, options]);
@@ -2779,6 +2800,7 @@ function eject(resourceName, id) {
27792800
_eject(_this.definitions[resourceName], resource, id);
27802801
resource.collectionModified = _this.utils.updateTimestamp(resource.collectionModified);
27812802
}
2803+
delete this.store[resourceName].completedQueries[id];
27822804
} catch (err) {
27832805
throw new this.errors.UnhandledError(err);
27842806
}

dist/angular-data.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datastore/async_methods/find/index.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var errorPrefix = 'DS.find(resourceName, id[, options]): ';
3030
* @param {string|number} id The primary key of the item to retrieve.
3131
* @param {object=} options Optional configuration. Properties:
3232
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
33+
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
34+
*
3335
* @returns {Promise} Promise produced by the `$q` service.
3436
*
3537
* ## Resolves with:
@@ -55,6 +57,11 @@ function find(resourceName, id, options) {
5557
} else if (!this.utils.isObject(options)) {
5658
deferred.reject(new this.errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } }));
5759
} else {
60+
if (!('cacheResponse' in options)) {
61+
options.cacheResponse = true;
62+
} else {
63+
options.cacheResponse = !!options.cacheResponse;
64+
}
5865
try {
5966
var definition = this.definitions[resourceName],
6067
resource = this.store[resourceName],
@@ -68,10 +75,14 @@ function find(resourceName, id, options) {
6875
if (!(id in resource.pendingQueries)) {
6976
promise = resource.pendingQueries[id] = _this.adapters[options.adapter || definition.defaultAdapter].find(definition, id, options)
7077
.then(function (data) {
71-
// Query is no longer pending
72-
delete resource.pendingQueries[id];
73-
resource.completedQueries[id] = new Date().getTime();
74-
return _this.inject(resourceName, data);
78+
if (options.cacheResponse) {
79+
// Query is no longer pending
80+
delete resource.pendingQueries[id];
81+
resource.completedQueries[id] = new Date().getTime();
82+
return _this.inject(resourceName, data);
83+
} else {
84+
return data;
85+
}
7586
});
7687
}
7788

src/datastore/async_methods/findAll/index.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ function _findAll(utils, resourceName, params, options) {
4040
// This particular query has never even been made
4141
resource.pendingQueries[queryHash] = _this.adapters[options.adapter || definition.defaultAdapter].findAll(definition, { params: params }, options)
4242
.then(function (data) {
43-
try {
44-
return processResults.apply(_this, [utils, data, resourceName, queryHash]);
45-
} catch (err) {
46-
throw new _this.errors.UnhandledError(err);
43+
if (options.cacheResponse) {
44+
try {
45+
return processResults.apply(_this, [utils, data, resourceName, queryHash]);
46+
} catch (err) {
47+
throw new _this.errors.UnhandledError(err);
48+
}
49+
} else {
50+
return data;
4751
}
4852
});
4953
}
@@ -106,6 +110,7 @@ function _findAll(utils, resourceName, params, options) {
106110
*
107111
* @param {object=} options Optional configuration. Properties:
108112
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
113+
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
109114
*
110115
* @returns {Promise} Promise produced by the `$q` service.
111116
*
@@ -133,6 +138,11 @@ function findAll(resourceName, params, options) {
133138
} else if (!this.utils.isObject(options)) {
134139
deferred.reject(new this.errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } }));
135140
} else {
141+
if (!('cacheResponse' in options)) {
142+
options.cacheResponse = true;
143+
} else {
144+
options.cacheResponse = !!options.cacheResponse;
145+
}
136146
try {
137147
promise = promise.then(function () {
138148
return _findAll.apply(_this, [_this.utils, resourceName, params, options]);

src/datastore/sync_methods/eject/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function eject(resourceName, id) {
7272
_eject(_this.definitions[resourceName], resource, id);
7373
resource.collectionModified = _this.utils.updateTimestamp(resource.collectionModified);
7474
}
75+
delete this.store[resourceName].completedQueries[id];
7576
} catch (err) {
7677
throw new this.errors.UnhandledError(err);
7778
}

test/integration/datastore/async_methods/find/index.test.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('DS.find(resourceName, id[, options]): ', function () {
22
var errorPrefix = 'DS.find(resourceName, id[, options]): ';
33

4-
it('should throw an error when method pre-conditions are not met', function (done) {
4+
it('should throw an error when method pre-conditions are not met', function () {
55
DS.find('does not exist', 5).then(function () {
66
fail('should have rejected');
77
}, function (err) {
@@ -28,10 +28,8 @@ describe('DS.find(resourceName, id[, options]): ', function () {
2828
});
2929
}
3030
});
31-
32-
done();
3331
});
34-
it('should get an item from the server', function (done) {
32+
it('should get an item from the server', function () {
3533
$httpBackend.expectGET('http://test.angular-cache.com/posts/5').respond(200, p1);
3634

3735
DS.find('post', 5).then(function (post) {
@@ -76,7 +74,19 @@ describe('DS.find(resourceName, id[, options]): ', function () {
7674
});
7775

7876
$httpBackend.flush();
77+
});
78+
it('should get an item from the server but not store it if cacheResponse is false', function () {
79+
$httpBackend.expectGET('http://test.angular-cache.com/posts/5').respond(200, p1);
80+
81+
DS.find('post', 5, { cacheResponse: false }).then(function (post) {
82+
assert.deepEqual(post, p1);
83+
}, function (err) {
84+
console.error(err.stack);
85+
fail('Should not have rejected!');
86+
});
87+
88+
$httpBackend.flush();
7989

80-
done();
90+
assert.isUndefined(DS.get('post', 5), 'The post should not have been injected into the store');
8191
});
8292
});

test/integration/datastore/async_methods/findAll/index.test.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
describe('DS.findAll(resourceName, params[, options]): ', function () {
22
var errorPrefix = 'DS.findAll(resourceName, params[, options]): ';
33

4-
it('should throw an error when method pre-conditions are not met', function (done) {
4+
it('should throw an error when method pre-conditions are not met', function () {
55
DS.findAll('does not exist', {}).then(function () {
66
fail('should have rejected');
77
}, function (err) {
@@ -28,10 +28,8 @@ describe('DS.findAll(resourceName, params[, options]): ', function () {
2828
});
2929
}
3030
});
31-
32-
done();
3331
});
34-
it('should query the server for a collection', function (done) {
32+
it('should query the server for a collection', function () {
3533
$httpBackend.expectGET('http://test.angular-cache.com/posts?').respond(200, [p1, p2, p3, p4]);
3634

3735
DS.findAll('post', {}).then(function (data) {
@@ -76,7 +74,19 @@ describe('DS.findAll(resourceName, params[, options]): ', function () {
7674
});
7775

7876
$httpBackend.flush();
77+
});
78+
it('should query the server for a collection but not store the data if cacheResponse is false', function () {
79+
$httpBackend.expectGET('http://test.angular-cache.com/posts?').respond(200, [p1, p2, p3, p4]);
80+
81+
DS.findAll('post', {}, { cacheResponse: false }).then(function (data) {
82+
assert.deepEqual(data, [p1, p2, p3, p4]);
83+
}, function (err) {
84+
console.error(err.stack);
85+
fail('Should not have rejected!');
86+
});
87+
88+
$httpBackend.flush();
7989

80-
done();
90+
assert.deepEqual(DS.filter('post', {}), [], 'The posts should not have been injected into the store');
8191
});
8292
});

0 commit comments

Comments
 (0)