Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make unloadAll() unload all records, deprecate unloadAll(type) in favor ... #2999

Merged
merged 2 commits into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions packages/ember-data/lib/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,31 +1049,46 @@ Store = Service.extend({
return array;
},


/**
This method unloads all of the known records for a given type.
This method unloads all records in the store.

```javascript
store.unloadAll('post');
```
Optionally you can pass a type which unload all records for a given type.

@method unloadAll
@param {String or subclass of DS.Model} type
```javascript
store.unloadAll();
store.unloadAll('post');
```

@method unloadAll
@param {String or subclass of DS.Model} optional type
*/
unloadAll: 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
if (arguments.length === 0) {
var typeMaps = this.typeMaps;
var keys = Ember.keys(typeMaps);

var types = map(keys, byType);

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);
}

typeMap.findAllCache = null;
typeMap.metadata = Ember.create(null);
function byType(entry) {
return typeMaps[entry]['type'];
}
},

/**
Expand Down Expand Up @@ -1917,18 +1932,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
36 changes: 35 additions & 1 deletion packages/ember-data/tests/integration/records/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,51 @@ 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.unloadAll('person');
});

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();
});

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 Down