Skip to content

Commit

Permalink
add store.unloadType(type) - was before unloadAll(type);
Browse files Browse the repository at this point in the history
change unloadAll behavior to: unload all data in the store
Related discussion emberjs#2988

Revert "fix issue emberjs#2988 (v1.0.0-beta.16.1 (and canary) - error after willDestroy)"

This reverts commit 2b599ad.
  • Loading branch information
svox1 committed Apr 13, 2015
1 parent dcaf77b commit ae946fa
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 19 deletions.
44 changes: 31 additions & 13 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,13 +1054,13 @@ Store = Service.extend({
This method unloads all of the known records for a given type.
```javascript
store.unloadAll('post');
store.unloadType('post');
```
@method unloadAll
@method unloadType
@param {String or subclass of DS.Model} type
*/
unloadAll: function(type) {
unloadType: function(type) {
var modelType = this.modelFor(type);
var typeMap = this.typeMapFor(modelType);
var records = typeMap.records.slice();
Expand All @@ -1076,6 +1076,33 @@ Store = Service.extend({
typeMap.metadata = Ember.create(null);
},

/**
This method unloads all records in the store.
```javascript
store.unloadAll();
```
@method unloadAll
*/
unloadAll: function(type) {
if (arguments.length === 1) {
Ember.deprecate('Using store.unloadAll(type) has been deprecated. You should use store.unloadType(type) instead.');
this.unloadType(type);
} else {
var typeMaps = this.typeMaps;
var keys = Ember.keys(typeMaps);

var types = map(keys, byType);

forEach(types, this.unloadType, this);
}

function byType(entry) {
return typeMaps[entry]['type'];
}
},

/**
Takes a type and filter function, and returns a live RecordArray that
remains up to date as new records are loaded into the store or created
Expand Down Expand Up @@ -1917,18 +1944,9 @@ Store = Service.extend({
},

willDestroy: function() {
var typeMaps = this.typeMaps;
var keys = Ember.keys(typeMaps);

var types = map(keys, byType);

this.recordArrayManager.destroy();

forEach(types, this.unloadAll, this);

function byType(entry) {
return typeMaps[entry]['type'];
}
this.unloadAll();

for (var cacheKey in this._containerCache) {
this._containerCache[cacheKey].destroy();
Expand Down
73 changes: 68 additions & 5 deletions packages/ember-data/tests/integration/records/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,80 @@ test("can unload a single record", function () {
});

test("can unload all records for a given type", function () {
var adam, bob;
expect(2);

var adam, bob, dudu;
run(function() {
adam = env.store.push('person', { id: 1, name: "Adam Sunderland" });
bob = env.store.push('person', { id: 2, name: "Bob Bobson" });

dudu = env.store.push('car', {
id: 1,
make: "VW",
model: "Beetle",
person: 1
});
});

Ember.run(function() {
env.store.unloadType('person');
});

equal(env.store.all('person').get('length'), 0);
equal(env.store.all('car').get('length'), 1);
});

test("Using store#unloadAll('type') is deprecated", function() {
expect(3);

var adam, bob, dudu;
run(function() {
adam = env.store.push('person', { id: 1, name: "Adam Sunderland" });
bob = env.store.push('person', { id: 2, name: "Bob Bobson" });

dudu = env.store.push('car', {
id: 1,
make: "VW",
model: "Beetle",
person: 1
});
});

expectDeprecation(
function() {
run(function() {
env.store.unloadAll('person');
});
},
'Using store.unloadAll(type) has been deprecated. You should use store.unloadType(type) instead.'
);

equal(env.store.all('person').get('length'), 0);
equal(env.store.all('car').get('length'), 1);
});

test("can unload all records", function () {
expect(2);

var adam, bob, dudu;
run(function() {
adam = env.store.push('person', { id: 1, name: "Adam Sunderland" });
bob = env.store.push('person', { id: 2, name: "Bob Bobson" });

dudu = env.store.push('car', {
id: 1,
make: "VW",
model: "Beetle",
person: 1
});
});

Ember.run(function() {
env.store.unloadAll('person');
env.store.unloadAll();
});

equal(env.store.all('person').get('length'), 0);
equal(env.store.all('car').get('length'), 0);
});

test("Unloading all records for a given type clears saved meta data.", function () {
Expand All @@ -79,7 +142,7 @@ test("Unloading all records for a given type clears saved meta data.", function
});

Ember.run(function() {
env.store.unloadAll('person');
env.store.unloadType('person');
});

deepEqual(metadataKeys('person'), [], 'Metadata for person is empty');
Expand All @@ -95,7 +158,7 @@ test("removes findAllCache after unloading all records", function () {

Ember.run(function() {
env.store.all('person');
env.store.unloadAll('person');
env.store.unloadType('person');
});

equal(env.store.all('person').get('length'), 0);
Expand All @@ -112,7 +175,7 @@ test("unloading all records also updates record array from all()", function() {
equal(all.get('length'), 2);

Ember.run(function() {
env.store.unloadAll('person');
env.store.unloadType('person');
});

equal(all.get('length'), 0);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-data/tests/integration/store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ asyncTest("destroying record during find doesn't cause error", function() {
find: function(store, type, id, snapshot) {
return new Ember.RSVP.Promise(function(resolve, reject) {
Ember.run.next(function() {
store.unloadAll(type);
store.unloadType(type);
reject();
});
});
Expand Down

0 comments on commit ae946fa

Please sign in to comment.