Skip to content

Commit 11bbff1

Browse files
committed
DS.ejectAll now clears the corresponding completed query. DS.eject is no longer capable of ejecting more than one item.
1 parent a7573fe commit 11bbff1

File tree

8 files changed

+66
-129
lines changed

8 files changed

+66
-129
lines changed

Gruntfile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,5 +279,5 @@ module.exports = function (grunt) {
279279
grunt.registerTask('default', ['build']);
280280

281281
// Used by TravisCI
282-
grunt.registerTask('ci', ['build', 'karma:ci', /*'coveralls', */'doc']);
282+
grunt.registerTask('ci', ['build', 'karma:ci', 'coveralls', 'doc']);
283283
};

dist/angular-data.js

+27-46
Original file line numberDiff line numberDiff line change
@@ -2561,31 +2561,22 @@ module.exports = digest;
25612561
var errorPrefix = 'DS.eject(resourceName, id): ';
25622562

25632563
function _eject(definition, resource, id) {
2564-
if (id) {
2565-
var found = false;
2566-
for (var i = 0; i < resource.collection.length; i++) {
2567-
if (resource.collection[i][definition.idAttribute] == id) {
2568-
found = true;
2569-
break;
2570-
}
2571-
}
2572-
if (found) {
2573-
resource.collection.splice(i, 1);
2574-
resource.observers[id].close();
2575-
delete resource.observers[id];
2576-
delete resource.index[id];
2577-
delete resource.changes[id];
2578-
delete resource.previousAttributes[id];
2579-
delete resource.modified[id];
2580-
delete resource.saved[id];
2564+
var found = false;
2565+
for (var i = 0; i < resource.collection.length; i++) {
2566+
if (resource.collection[i][definition.idAttribute] == id) {
2567+
found = true;
2568+
break;
25812569
}
2582-
} else {
2583-
resource.collection = [];
2584-
resource.index = {};
2585-
resource.modified = {};
2586-
resource.saved = {};
2587-
resource.changes = {};
2588-
resource.previousAttributes = {};
2570+
}
2571+
if (found) {
2572+
resource.collection.splice(i, 1);
2573+
resource.observers[id].close();
2574+
delete resource.observers[id];
2575+
delete resource.index[id];
2576+
delete resource.changes[id];
2577+
delete resource.previousAttributes[id];
2578+
delete resource.modified[id];
2579+
delete resource.saved[id];
25892580
}
25902581
}
25912582

@@ -2594,16 +2585,15 @@ function _eject(definition, resource, id) {
25942585
* @id DS.sync_methods:eject
25952586
* @name eject
25962587
* @description
2597-
* Eject the item of the specified type that has the given primary key from the data store. If no primary key is
2598-
* provided, eject all items of the specified type from the data store. Ejection only removes items from the data store
2599-
* and does not attempt to delete items on the server.
2588+
* Eject the item of the specified type that has the given primary key from the data store. Ejection only removes items
2589+
* from the data store and does not attempt to delete items on the server.
26002590
*
26012591
* ## Signature:
26022592
* ```js
26032593
* DS.eject(resourceName[, id])
26042594
* ```
26052595
*
2606-
* ## Examples:
2596+
* ## Example:
26072597
*
26082598
* ```js
26092599
* DS.get('document', 45); // { title: 'How to Cook', id: 45 }
@@ -2613,29 +2603,19 @@ function _eject(definition, resource, id) {
26132603
* DS.get('document', 45); // undefined
26142604
* ```
26152605
*
2616-
* Eject all items of the specified type from the data store.
2617-
*
2618-
* ```js
2619-
* DS.filter('document'); // [ { title: 'How to Cook', id: 45 }, { title: 'How to Eat', id: 46 } ]
2620-
*
2621-
* DS.eject('document');
2622-
*
2623-
* DS.filter('document'); // [ ]
2624-
* ```
2625-
*
26262606
* ## Throws
26272607
*
26282608
* - `{IllegalArgumentError}`
26292609
* - `{RuntimeError}`
26302610
* - `{UnhandledError}`
26312611
*
26322612
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
2633-
* @param {string|number=} id The primary key of the item to eject.
2613+
* @param {string|number} id The primary key of the item to eject.
26342614
*/
26352615
function eject(resourceName, id) {
26362616
if (!this.definitions[resourceName]) {
26372617
throw new this.errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!');
2638-
} else if (id && !this.utils.isString(id) && !this.utils.isNumber(id)) {
2618+
} else if (!this.utils.isString(id) && !this.utils.isNumber(id)) {
26392619
throw new this.errors.IllegalArgumentError(errorPrefix + 'id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
26402620
}
26412621

@@ -2662,14 +2642,15 @@ module.exports = eject;
26622642
},{}],40:[function(require,module,exports){
26632643
var errorPrefix = 'DS.ejectAll(resourceName[, params]): ';
26642644

2665-
function _ejectAll(definition, params) {
2666-
params.query = params.query || {};
2667-
2668-
var items = this.filter(definition.name, params);
2645+
function _ejectAll(definition, resource, params) {
2646+
var queryHash = this.utils.toJson(params),
2647+
items = this.filter(definition.name, params);
26692648

26702649
for (var i = 0; i < items.length; i++) {
26712650
this.eject(definition.name, items[i][definition.idAttribute]);
26722651
}
2652+
2653+
delete resource.completedQueries[queryHash];
26732654
}
26742655

26752656
/**
@@ -2748,11 +2729,11 @@ function ejectAll(resourceName, params) {
27482729
try {
27492730
if (!this.$rootScope.$$phase) {
27502731
this.$rootScope.$apply(function () {
2751-
_ejectAll.apply(_this, [_this.definitions[resourceName], params]);
2732+
_ejectAll.apply(_this, [_this.definitions[resourceName], resource, params]);
27522733
resource.collectionModified = _this.utils.updateTimestamp(resource.collectionModified);
27532734
});
27542735
} else {
2755-
_ejectAll.apply(_this, [_this.definitions[resourceName], params]);
2736+
_ejectAll.apply(_this, [_this.definitions[resourceName], resource, params]);
27562737
resource.collectionModified = this.utils.updateTimestamp(resource.collectionModified);
27572738
}
27582739
} catch (err) {

dist/angular-data.min.js

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

karma.conf.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module.exports = function (config) {
1111
'karma-chai',
1212
'karma-chrome-launcher',
1313
'karma-phantomjs-launcher',
14-
'karma-firefox-launcher'//,
15-
// 'karma-coverage'
14+
'karma-firefox-launcher',
15+
'karma-coverage'
1616
],
1717
autoWatch: false,
1818
browsers: ['Chrome'],
@@ -26,11 +26,11 @@ module.exports = function (config) {
2626
'karma.start.js'
2727
],
2828

29-
reporters: ['progress'/*, 'coverage'*/],
29+
reporters: ['progress', 'coverage'],
3030

31-
// preprocessors: {
32-
// 'dist/angular-data.js': ['coverage']
33-
// },
31+
preprocessors: {
32+
'dist/angular-data.js': ['coverage']
33+
},
3434

3535
// optionally, configure the reporter
3636
coverageReporter: {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"grunt-karma": "~0.6.2",
4343
"karma-chai": "0.1.0",
4444
"mocha": "~1.17.1",
45-
"karma-coverage": "~0.1.4",
45+
"karma-coverage": "~0.1.2",
4646
"grunt-contrib-watch": "~0.5.3",
4747
"grunt-karma-coveralls": "~2.3.0",
4848
"karma-mocha": "~0.1.1",

src/datastore/sync_methods/eject/index.js

+20-40
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
var errorPrefix = 'DS.eject(resourceName, id): ';
22

33
function _eject(definition, resource, id) {
4-
if (id) {
5-
var found = false;
6-
for (var i = 0; i < resource.collection.length; i++) {
7-
if (resource.collection[i][definition.idAttribute] == id) {
8-
found = true;
9-
break;
10-
}
4+
var found = false;
5+
for (var i = 0; i < resource.collection.length; i++) {
6+
if (resource.collection[i][definition.idAttribute] == id) {
7+
found = true;
8+
break;
119
}
12-
if (found) {
13-
resource.collection.splice(i, 1);
14-
resource.observers[id].close();
15-
delete resource.observers[id];
16-
delete resource.index[id];
17-
delete resource.changes[id];
18-
delete resource.previousAttributes[id];
19-
delete resource.modified[id];
20-
delete resource.saved[id];
21-
}
22-
} else {
23-
resource.collection = [];
24-
resource.index = {};
25-
resource.modified = {};
26-
resource.saved = {};
27-
resource.changes = {};
28-
resource.previousAttributes = {};
10+
}
11+
if (found) {
12+
resource.collection.splice(i, 1);
13+
resource.observers[id].close();
14+
delete resource.observers[id];
15+
delete resource.index[id];
16+
delete resource.changes[id];
17+
delete resource.previousAttributes[id];
18+
delete resource.modified[id];
19+
delete resource.saved[id];
2920
}
3021
}
3122

@@ -34,16 +25,15 @@ function _eject(definition, resource, id) {
3425
* @id DS.sync_methods:eject
3526
* @name eject
3627
* @description
37-
* Eject the item of the specified type that has the given primary key from the data store. If no primary key is
38-
* provided, eject all items of the specified type from the data store. Ejection only removes items from the data store
39-
* and does not attempt to delete items on the server.
28+
* Eject the item of the specified type that has the given primary key from the data store. Ejection only removes items
29+
* from the data store and does not attempt to delete items on the server.
4030
*
4131
* ## Signature:
4232
* ```js
4333
* DS.eject(resourceName[, id])
4434
* ```
4535
*
46-
* ## Examples:
36+
* ## Example:
4737
*
4838
* ```js
4939
* DS.get('document', 45); // { title: 'How to Cook', id: 45 }
@@ -53,29 +43,19 @@ function _eject(definition, resource, id) {
5343
* DS.get('document', 45); // undefined
5444
* ```
5545
*
56-
* Eject all items of the specified type from the data store.
57-
*
58-
* ```js
59-
* DS.filter('document'); // [ { title: 'How to Cook', id: 45 }, { title: 'How to Eat', id: 46 } ]
60-
*
61-
* DS.eject('document');
62-
*
63-
* DS.filter('document'); // [ ]
64-
* ```
65-
*
6646
* ## Throws
6747
*
6848
* - `{IllegalArgumentError}`
6949
* - `{RuntimeError}`
7050
* - `{UnhandledError}`
7151
*
7252
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
73-
* @param {string|number=} id The primary key of the item to eject.
53+
* @param {string|number} id The primary key of the item to eject.
7454
*/
7555
function eject(resourceName, id) {
7656
if (!this.definitions[resourceName]) {
7757
throw new this.errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!');
78-
} else if (id && !this.utils.isString(id) && !this.utils.isNumber(id)) {
58+
} else if (!this.utils.isString(id) && !this.utils.isNumber(id)) {
7959
throw new this.errors.IllegalArgumentError(errorPrefix + 'id: Must be a string or a number!', { id: { actual: typeof id, expected: 'string|number' } });
8060
}
8161

src/datastore/sync_methods/ejectAll/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
var errorPrefix = 'DS.ejectAll(resourceName[, params]): ';
22

3-
function _ejectAll(definition, params) {
4-
params.query = params.query || {};
5-
6-
var items = this.filter(definition.name, params);
3+
function _ejectAll(definition, resource, params) {
4+
var queryHash = this.utils.toJson(params),
5+
items = this.filter(definition.name, params);
76

87
for (var i = 0; i < items.length; i++) {
98
this.eject(definition.name, items[i][definition.idAttribute]);
109
}
10+
11+
delete resource.completedQueries[queryHash];
1112
}
1213

1314
/**
@@ -86,11 +87,11 @@ function ejectAll(resourceName, params) {
8687
try {
8788
if (!this.$rootScope.$$phase) {
8889
this.$rootScope.$apply(function () {
89-
_ejectAll.apply(_this, [_this.definitions[resourceName], params]);
90+
_ejectAll.apply(_this, [_this.definitions[resourceName], resource, params]);
9091
resource.collectionModified = _this.utils.updateTimestamp(resource.collectionModified);
9192
});
9293
} else {
93-
_ejectAll.apply(_this, [_this.definitions[resourceName], params]);
94+
_ejectAll.apply(_this, [_this.definitions[resourceName], resource, params]);
9495
resource.collectionModified = this.utils.updateTimestamp(resource.collectionModified);
9596
}
9697
} catch (err) {

test/integration/datastore/sync_methods/eject/index.test.js

+3-28
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ describe('DS.eject(resourceName, id)', function () {
77
}, DS.errors.RuntimeError, errorPrefix + 'does not exist is not a registered resource!');
88

99
angular.forEach(TYPES_EXCEPT_STRING_OR_NUMBER, function (key) {
10-
if (key) {
11-
assert.throws(function () {
12-
DS.eject('post', key);
13-
}, DS.errors.IllegalArgumentError, errorPrefix + 'id: Must be a string or a number!');
14-
}
10+
assert.throws(function () {
11+
DS.eject('post', key);
12+
}, DS.errors.IllegalArgumentError, errorPrefix + 'id: Must be a string or a number!');
1513
});
1614

1715
done();
@@ -38,29 +36,6 @@ describe('DS.eject(resourceName, id)', function () {
3836
assert.isUndefined(DS.get('post', 5));
3937
assert.equal(DS.lastModified('post', 5), 0);
4038

41-
done();
42-
});
43-
it('should eject all items from the store', function (done) {
44-
45-
DS.inject('post', p1);
46-
DS.inject('post', p2);
47-
DS.inject('post', p3);
48-
DS.inject('post', p4);
49-
50-
assert.deepEqual(DS.get('post', 5), p1);
51-
assert.deepEqual(DS.get('post', 6), p2);
52-
assert.deepEqual(DS.get('post', 7), p3);
53-
assert.deepEqual(DS.get('post', 8), p4);
54-
55-
assert.doesNotThrow(function () {
56-
DS.eject('post');
57-
});
58-
59-
assert.isUndefined(DS.get('post', 5));
60-
assert.isUndefined(DS.get('post', 6));
61-
assert.isUndefined(DS.get('post', 7));
62-
assert.isUndefined(DS.get('post', 8));
63-
6439
done();
6540
});
6641
});

0 commit comments

Comments
 (0)