From 64369188945dc03d7f1c390c8003e72da95b8cf5 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 13 Apr 2015 20:51:58 +0200 Subject: [PATCH 1/2] Make unloadAll() unload all records, deprecate unloadAll(type) in favor of unloadType(type)e Related discussion #2988 --- packages/ember-data/lib/system/store.js | 44 +++++++---- .../tests/integration/records/unload-test.js | 73 +++++++++++++++++-- .../tests/integration/store-test.js | 2 +- 3 files changed, 100 insertions(+), 19 deletions(-) diff --git a/packages/ember-data/lib/system/store.js b/packages/ember-data/lib/system/store.js index 0422d07358b..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,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(); 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(); }); }); From ea108b5645d9cde473afa34a32f6f10f5f07f62b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 27 Apr 2015 20:09:53 +0200 Subject: [PATCH 2/2] revert new method unloadType and implement logic in unloadAll(type); / unloadAll(); --- packages/ember-data/lib/system/store.js | 52 +++++++------------ .../tests/integration/records/unload-test.js | 37 ++----------- .../tests/integration/store-test.js | 2 +- 3 files changed, 25 insertions(+), 66 deletions(-) diff --git a/packages/ember-data/lib/system/store.js b/packages/ember-data/lib/system/store.js index 87408573163..9ab45a64247 100644 --- a/packages/ember-data/lib/system/store.js +++ b/packages/ember-data/lib/system/store.js @@ -1049,53 +1049,41 @@ Store = Service.extend({ return array; }, - - /** - This method unloads all of the known records for a given type. - - ```javascript - store.unloadType('post'); - ``` - - @method unloadType - @param {String or subclass of DS.Model} type - */ - unloadType: function(type) { - var modelType = this.modelFor(type); - var typeMap = this.typeMapFor(modelType); - var records = typeMap.records.slice(); - var record; - - for (var i = 0; i < records.length; i++) { - record = records[i]; - record.unloadRecord(); - record.destroy(); // maybe within unloadRecord - } - - typeMap.findAllCache = null; - typeMap.metadata = Ember.create(null); - }, - /** This method unloads all records in the store. + Optionally you can pass a type which unload all records for a given type. + ```javascript store.unloadAll(); + store.unloadAll('post'); ``` @method unloadAll + @param {String or subclass of DS.Model} optional type */ 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 { + if (arguments.length === 0) { var typeMaps = this.typeMaps; var keys = Ember.keys(typeMaps); var types = map(keys, byType); - forEach(types, this.unloadType, this); + forEach(types, this.unloadAll, this); + } else { + var modelType = this.modelFor(type); + var typeMap = this.typeMapFor(modelType); + var records = typeMap.records.slice(); + var record; + + for (var i = 0; i < records.length; i++) { + record = records[i]; + record.unloadRecord(); + record.destroy(); // maybe within unloadRecord + } + + typeMap.findAllCache = null; + typeMap.metadata = Ember.create(null); } function byType(entry) { diff --git a/packages/ember-data/tests/integration/records/unload-test.js b/packages/ember-data/tests/integration/records/unload-test.js index 89969c1cae6..0e5008cfb39 100644 --- a/packages/ember-data/tests/integration/records/unload-test.js +++ b/packages/ember-data/tests/integration/records/unload-test.js @@ -71,42 +71,13 @@ test("can unload all records for a given type", function () { }); Ember.run(function() { - env.store.unloadType('person'); + env.store.unloadAll('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); @@ -142,7 +113,7 @@ test("Unloading all records for a given type clears saved meta data.", function }); Ember.run(function() { - env.store.unloadType('person'); + env.store.unloadAll('person'); }); deepEqual(metadataKeys('person'), [], 'Metadata for person is empty'); @@ -158,7 +129,7 @@ test("removes findAllCache after unloading all records", function () { Ember.run(function() { env.store.all('person'); - env.store.unloadType('person'); + env.store.unloadAll('person'); }); equal(env.store.all('person').get('length'), 0); @@ -175,7 +146,7 @@ test("unloading all records also updates record array from all()", function() { equal(all.get('length'), 2); Ember.run(function() { - env.store.unloadType('person'); + env.store.unloadAll('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 adffed78661..864c464c9b8 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.unloadType(type); + store.unloadAll(type); reject(); }); });