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
  • Loading branch information
svox1 committed Apr 13, 2015
1 parent 19227fb commit aad7ee1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 18 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
39 changes: 35 additions & 4 deletions packages/ember-data/tests/integration/records/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,38 @@ test("can unload all records for a given type", function () {
});

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

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

test("Using store#unloadAll('type') is deprecated", function() {
var adam, bob;
run(function() {
adam = env.store.push('person', { id: 1, name: "Adam Sunderland" });
bob = env.store.push('person', { id: 2, name: "Bob Bobson" });
});

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

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

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

equal(env.store.all('person').get('length'), 0);
Expand All @@ -79,7 +110,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 +126,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 +143,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 aad7ee1

Please sign in to comment.