diff --git a/packages/ember-data/lib/system/store.js b/packages/ember-data/lib/system/store.js index c0c0219ee23..87408573163 100644 --- a/packages/ember-data/lib/system/store.js +++ b/packages/ember-data/lib/system/store.js @@ -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(); @@ -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 @@ -1917,25 +1944,16 @@ 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(); delete this._containerCache[cacheKey]; } - this._containerCache = Ember.create(null); + delete this._containerCache; }, /** diff --git a/packages/ember-data/tests/integration/records/unload-test.js b/packages/ember-data/tests/integration/records/unload-test.js index 75f609ca054..89969c1cae6 100644 --- a/packages/ember-data/tests/integration/records/unload-test.js +++ b/packages/ember-data/tests/integration/records/unload-test.js @@ -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 () { @@ -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'); @@ -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); @@ -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); diff --git a/packages/ember-data/tests/integration/store-test.js b/packages/ember-data/tests/integration/store-test.js index 864c464c9b8..adffed78661 100644 --- a/packages/ember-data/tests/integration/store-test.js +++ b/packages/ember-data/tests/integration/store-test.js @@ -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(); }); });