Skip to content

Commit

Permalink
Merge pull request emberjs#2807 from emberjs/cleanup_implicit_on_unload
Browse files Browse the repository at this point in the history
CLean up implicit relationships on record unload
  • Loading branch information
igorT committed Feb 20, 2015
2 parents f49c2a7 + b32bd9c commit 8d00fae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
5 changes: 5 additions & 0 deletions packages/ember-data/lib/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@ var Model = Ember.Object.extend(Ember.Evented, {
rel.destroy();
}
}, this);
var model = this;
forEach.call(Ember.keys(this._implicitRelationships), function(key) {
model._implicitRelationships[key].clear();
model._implicitRelationships[key].destroy();
});
},

disconnectRelationships: function() {
Expand Down
45 changes: 41 additions & 4 deletions packages/ember-data/tests/integration/records/unload_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ var Person = DS.Model.extend({
cars: hasMany('car')
});

Person.toString = function() { return "Person"; };

var Group = DS.Model.extend({
people: hasMany('person')
});

Group.toString = function() { return "Group"; };

var Car = DS.Model.extend({
make: attr('string'),
model: attr('string'),
person: belongsTo('person')
});

Person.toString = function() { return "Person"; };
Car.toString = function() { return "Car"; };

module("integration/unload - Unloading Records", {
setup: function() {
env = setupStore({
person: Person,
car: Car
car: Car,
group: Group
});
},

Expand Down Expand Up @@ -92,7 +101,6 @@ test("unloading all records also updates record array from all()", function() {
});


//TODO(Igor) think about how this works with ssot and unloading
test("unloading a record also clears its relationship", function() {
var adam, bob;
run(function() {
Expand All @@ -114,7 +122,7 @@ test("unloading a record also clears its relationship", function() {

run(function() {
env.store.find('person', 1).then(function(person) {
equal(person.get('cars.length'), 1, 'aaaa');
equal(person.get('cars.length'), 1, 'The inital length of cars is correct');

run(function() {
person.unloadRecord();
Expand All @@ -124,3 +132,32 @@ test("unloading a record also clears its relationship", function() {
});
});
});

test("unloading a record also clears the implicit inverse relationships", function() {
var adam, bob;
run(function() {
adam = env.store.push('person', {
id: 1,
name: "Adam Sunderland"
});
});

run(function() {
bob = env.store.push('group', {
id: 1,
people: [1]
});
});

run(function() {
env.store.find('group', 1).then(function(group) {
equal(group.get('people.length'), 1, 'The inital length of people is correct');
var person = env.store.getById('person', 1);
run(function() {
person.unloadRecord();
});

equal(group.get('people.length'), 0, 'Person was removed from the people array');
});
});
});

0 comments on commit 8d00fae

Please sign in to comment.