Skip to content

Commit 39f094c

Browse files
committed
Fixes #70
1 parent 17cdf07 commit 39f094c

File tree

10 files changed

+71
-37
lines changed

10 files changed

+71
-37
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
###### Backwards compatible bug fixes
44
- #69 - Failed requests should throw an error, not just return it
5+
- #70 - Make `params` and `params.query` optional
56

67
##### 0.9.0 - 22 May 2014
78

dist/angular-data.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @author Jason Dobry <[email protected]>
33
* @file angular-data.js
4-
* @version 0.9.0 - Homepage <http://angular-data.codetrain.io/>
4+
* @version 0.9.1 - Homepage <http://angular-data.codetrain.io/>
55
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
66
* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
77
*
@@ -1570,7 +1570,9 @@ function DSHttpAdapterProvider() {
15701570
options = options || {};
15711571
options.params = options.params || {};
15721572
if (params) {
1573-
params.query = params.query ? defaults.queryTransform(resourceConfig.name, params.query) : params.query;
1573+
if (params.query) {
1574+
params.query = defaults.queryTransform(resourceConfig.name, params.query);
1575+
}
15741576
DSUtils.deepMixIn(options.params, params);
15751577
}
15761578
return this.DEL(
@@ -1591,7 +1593,9 @@ function DSHttpAdapterProvider() {
15911593
options = options || {};
15921594
options.params = options.params || {};
15931595
if (params) {
1594-
params.query = params.query ? defaults.queryTransform(resourceConfig.name, params.query) : params.query;
1596+
if (params.query) {
1597+
params.query = defaults.queryTransform(resourceConfig.name, params.query);
1598+
}
15951599
DSUtils.deepMixIn(options.params, params);
15961600
}
15971601
return this.GET(
@@ -1613,7 +1617,9 @@ function DSHttpAdapterProvider() {
16131617
options = options || {};
16141618
options.params = options.params || {};
16151619
if (params) {
1616-
params.query = params.query ? defaults.queryTransform(resourceConfig.name, params.query) : params.query;
1620+
if (params.query) {
1621+
params.query = defaults.queryTransform(resourceConfig.name, params.query);
1622+
}
16171623
DSUtils.deepMixIn(options.params, params);
16181624
}
16191625
return this.PUT(
@@ -2302,7 +2308,7 @@ function _findAll(utils, resourceName, params, options) {
23022308
* ```
23032309
*
23042310
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
2305-
* @param {object} params Parameter object that is serialized into the query string. Properties:
2311+
* @param {object=} params Parameter object that is serialized into the query string. Properties:
23062312
*
23072313
* - `{object=}` - `query` - The query object by which to filter items of the type specified by `resourceName`. Properties:
23082314
* - `{object=}` - `where` - Where clause.
@@ -2332,6 +2338,7 @@ function findAll(resourceName, params, options) {
23322338
_this = this;
23332339

23342340
options = options || {};
2341+
params = params || {};
23352342

23362343
if (!this.definitions[resourceName]) {
23372344
deferred.reject(new this.errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!'));
@@ -4048,7 +4055,7 @@ var errorPrefix = 'DS.filter(resourceName, params[, options]): ';
40484055
* - `{UnhandledError}`
40494056
*
40504057
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
4051-
* @param {object} params Parameter object that is serialized into the query string. Properties:
4058+
* @param {object=} params Parameter object that is serialized into the query string. Properties:
40524059
*
40534060
* - `{object=}` - `query` - The query object by which to filter items of the type specified by `resourceName`. Properties:
40544061
* - `{object=}` - `where` - Where clause.
@@ -4065,7 +4072,7 @@ function filter(resourceName, params, options) {
40654072

40664073
if (!this.definitions[resourceName]) {
40674074
throw new this.errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!');
4068-
} else if (!this.utils.isObject(params)) {
4075+
} else if (params && !this.utils.isObject(params)) {
40694076
throw new this.errors.IllegalArgumentError(errorPrefix + 'params: Must be an object!', { params: { actual: typeof params, expected: 'object' } });
40704077
} else if (!this.utils.isObject(options)) {
40714078
throw new this.errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } });
@@ -4077,7 +4084,7 @@ function filter(resourceName, params, options) {
40774084
_this = this;
40784085

40794086
// Protect against null
4080-
params.query = params.query || {};
4087+
params = params || {};
40814088

40824089
var queryHash = this.utils.toJson(params);
40834090

@@ -4090,6 +4097,7 @@ function filter(resourceName, params, options) {
40904097
}
40914098
}
40924099

4100+
params.query = params.query || {};
40934101
// The query has been completed, so hit the cache with the query
40944102
var filtered = this.utils.filter(resource.collection, function (attrs) {
40954103
var keep = true,

dist/angular-data.min.js

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

src/adapters/http.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ function DSHttpAdapterProvider() {
369369
options = options || {};
370370
options.params = options.params || {};
371371
if (params) {
372-
params.query = params.query ? defaults.queryTransform(resourceConfig.name, params.query) : params.query;
372+
if (params.query) {
373+
params.query = defaults.queryTransform(resourceConfig.name, params.query);
374+
}
373375
DSUtils.deepMixIn(options.params, params);
374376
}
375377
return this.DEL(
@@ -390,7 +392,9 @@ function DSHttpAdapterProvider() {
390392
options = options || {};
391393
options.params = options.params || {};
392394
if (params) {
393-
params.query = params.query ? defaults.queryTransform(resourceConfig.name, params.query) : params.query;
395+
if (params.query) {
396+
params.query = defaults.queryTransform(resourceConfig.name, params.query);
397+
}
394398
DSUtils.deepMixIn(options.params, params);
395399
}
396400
return this.GET(
@@ -412,7 +416,9 @@ function DSHttpAdapterProvider() {
412416
options = options || {};
413417
options.params = options.params || {};
414418
if (params) {
415-
params.query = params.query ? defaults.queryTransform(resourceConfig.name, params.query) : params.query;
419+
if (params.query) {
420+
params.query = defaults.queryTransform(resourceConfig.name, params.query);
421+
}
416422
DSUtils.deepMixIn(options.params, params);
417423
}
418424
return this.PUT(

src/datastore/async_methods/findAll.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ function _findAll(utils, resourceName, params, options) {
9999
* ```
100100
*
101101
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
102-
* @param {object} params Parameter object that is serialized into the query string. Properties:
102+
* @param {object=} params Parameter object that is serialized into the query string. Properties:
103103
*
104104
* - `{object=}` - `query` - The query object by which to filter items of the type specified by `resourceName`. Properties:
105105
* - `{object=}` - `where` - Where clause.
@@ -129,6 +129,7 @@ function findAll(resourceName, params, options) {
129129
_this = this;
130130

131131
options = options || {};
132+
params = params || {};
132133

133134
if (!this.definitions[resourceName]) {
134135
deferred.reject(new this.errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!'));

src/datastore/sync_methods/filter.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ var errorPrefix = 'DS.filter(resourceName, params[, options]): ';
2626
* - `{UnhandledError}`
2727
*
2828
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
29-
* @param {object} params Parameter object that is serialized into the query string. Properties:
29+
* @param {object=} params Parameter object that is serialized into the query string. Properties:
3030
*
3131
* - `{object=}` - `query` - The query object by which to filter items of the type specified by `resourceName`. Properties:
3232
* - `{object=}` - `where` - Where clause.
@@ -43,7 +43,7 @@ function filter(resourceName, params, options) {
4343

4444
if (!this.definitions[resourceName]) {
4545
throw new this.errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!');
46-
} else if (!this.utils.isObject(params)) {
46+
} else if (params && !this.utils.isObject(params)) {
4747
throw new this.errors.IllegalArgumentError(errorPrefix + 'params: Must be an object!', { params: { actual: typeof params, expected: 'object' } });
4848
} else if (!this.utils.isObject(options)) {
4949
throw new this.errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!', { options: { actual: typeof options, expected: 'object' } });
@@ -55,7 +55,7 @@ function filter(resourceName, params, options) {
5555
_this = this;
5656

5757
// Protect against null
58-
params.query = params.query || {};
58+
params = params || {};
5959

6060
var queryHash = this.utils.toJson(params);
6161

@@ -68,6 +68,7 @@ function filter(resourceName, params, options) {
6868
}
6969
}
7070

71+
params.query = params.query || {};
7172
// The query has been completed, so hit the cache with the query
7273
var filtered = this.utils.filter(resource.collection, function (attrs) {
7374
var keep = true,

test/integration/adapters/http/destroyAll.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('DSHttpAdapter.destroyAll(resourceConfig, params, options)', function (
88
DSHttpAdapter.destroyAll({
99
baseUrl: 'api',
1010
endpoint: 'posts'
11-
}).then(function (data) {
11+
}, {}).then(function (data) {
1212
assert.isUndefined(data.data, 'posts should have been found');
1313
}, function (err) {
1414
console.error(err.stack);

test/integration/adapters/http/findAll.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('DSHttpAdapter.findAll(resourceConfig, params, options)', function () {
88
DSHttpAdapter.findAll({
99
baseUrl: 'api',
1010
endpoint: 'posts'
11-
}).then(function (data) {
11+
}, {}).then(function (data) {
1212
assert.deepEqual(data.data, [p1], 'posts should have been found');
1313
}, function (err) {
1414
console.error(err.stack);

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

+27-6
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ describe('DS.findAll(resourceName, params[, options]): ', function () {
1212
});
1313

1414
angular.forEach(TYPES_EXCEPT_OBJECT, function (key) {
15-
DS.findAll('post', key).then(function () {
16-
fail('should have rejected');
17-
}, function (err) {
18-
assert.isTrue(err instanceof DS.errors.IllegalArgumentError);
19-
assert.equal(err.message, errorPrefix + 'params: Must be an object!');
20-
});
15+
if (key) {
16+
DS.findAll('post', key, { cacheResponse: false }).then(function () {
17+
fail('should have rejected');
18+
}, function (err) {
19+
assert.isTrue(err instanceof DS.errors.IllegalArgumentError);
20+
assert.equal(err.message, errorPrefix + 'params: Must be an object!');
21+
});
22+
}
2123
});
2224

2325
angular.forEach(TYPES_EXCEPT_OBJECT, function (key) {
@@ -112,4 +114,23 @@ describe('DS.findAll(resourceName, params[, options]): ', function () {
112114

113115
$httpBackend.flush();
114116
});
117+
it('"params" argument is optional', function () {
118+
$httpBackend.expectGET(/http:\/\/test\.angular-cache\.com\/posts\??/).respond(200, [p1, p2, p3, p4]);
119+
120+
DS.findAll('post').then(function (data) {
121+
assert.deepEqual(data, [p1, p2, p3, p4]);
122+
}, function (err) {
123+
console.error(err.message);
124+
fail('Should not have rejected!');
125+
});
126+
127+
$httpBackend.flush();
128+
129+
assert.deepEqual(DS.filter('post', {}), [p1, p2, p3, p4], 'The posts are now in the store');
130+
131+
assert.equal(lifecycle.beforeInject.callCount, 4, 'beforeInject should have been called');
132+
assert.equal(lifecycle.afterInject.callCount, 4, 'afterInject should have been called');
133+
assert.equal(lifecycle.serialize.callCount, 0, 'serialize should have been called');
134+
assert.equal(lifecycle.deserialize.callCount, 1, 'deserialize should have been called');
135+
});
115136
});

test/integration/datastore/sync_methods/filter.test.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@ describe('DS.filter(resourceName, params[, options])', function () {
55

66
it('should throw an error when method pre-conditions are not met', function () {
77
assert.throws(function () {
8-
DS.filter('does not exist', {});
8+
DS.filter('does not exist');
99
}, DS.errors.RuntimeError, errorPrefix + 'does not exist is not a registered resource!');
1010

1111
angular.forEach(TYPES_EXCEPT_OBJECT, function (key) {
12-
assert.throws(function () {
13-
DS.filter('post', key);
14-
}, DS.errors.IllegalArgumentError, errorPrefix + 'params: Must be an object!');
15-
});
16-
17-
angular.forEach(TYPES_EXCEPT_OBJECT, function (key) {
18-
assert.throws(function () {
19-
DS.filter('post', key);
20-
}, DS.errors.IllegalArgumentError, errorPrefix + 'params: Must be an object!');
12+
if (key) {
13+
assert.throws(function () {
14+
DS.filter('post', key);
15+
}, DS.errors.IllegalArgumentError, errorPrefix + 'params: Must be an object!');
16+
}
2117
});
2218

2319
DS.inject('post', p1);
@@ -66,7 +62,7 @@ describe('DS.filter(resourceName, params[, options])', function () {
6662
}
6763
});
6864

69-
DS.filter('post', {});
65+
DS.filter('post');
7066
});
7167
it('should return an empty array if the query has never been made before', function () {
7268
$httpBackend.expectGET('http://test.angular-cache.com/posts?query=%7B%22where%22:%7B%22author%22:%7B%22%3D%3D%22:%22John%22%7D%7D%7D').respond(200, [p1]);

0 commit comments

Comments
 (0)