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

[BUGFIX release] Guard against isDestroyed in ManyArray.flushCanonical #3559

Merged
merged 1 commit into from
Mar 9, 2016
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
6 changes: 5 additions & 1 deletion addon/-private/system/many-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import Ember from 'ember';
import { assert } from "ember-data/-private/debug";
import { PromiseArray } from "ember-data/-private/system/promise-proxies";
import { _objectIsAlive } from "ember-data/-private/system/store/common";

var get = Ember.get;
var set = Ember.set;
Expand Down Expand Up @@ -84,7 +85,10 @@ export default Ember.Object.extend(Ember.MutableArray, Ember.Evented, {
toSet = toSet.concat(newRecords);
var oldLength = this.length;
this.arrayContentWillChange(0, this.length, toSet.length);
this.set('length', toSet.length);
// It’s possible the parent side of the relationship may have been unloaded by this point
if (_objectIsAlive(this)) {
this.set('length', toSet.length);
}
this.currentState = toSet;
this.arrayContentDidChange(0, oldLength, this.length);
//TODO Figure out to notify only on additions and maybe only if unloaded
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2700,3 +2700,65 @@ test("PromiseArray proxies createRecord to its ManyArray before the hasMany is l
});
});
});

test("unloading and reloading a record with hasMany relationship - #3084", function(assert) {
var user;
var message;

run(function() {
env.store.push({
data: [{
type: 'user',
id: 'user-1',
attributes: {
name: 'Adolfo Builes'
},
relationships: {
messages: {
data: [
{ type: 'message', id: 'message-1' }
]
}
}
}, {
type: 'message',
id: 'message-1'
}]
});

user = env.store.peekRecord('user', 'user-1');
message = env.store.peekRecord('message', 'message-1');

assert.equal(get(user, 'messages.firstObject.id'), 'message-1');
assert.equal(get(message, 'user.id'), 'user-1');
});

run(function() {
env.store.unloadRecord(user);
});

run(function() {
// The record is resurrected for some reason.
env.store.push({
data: [{
type: 'user',
id: 'user-1',
attributes: {
name: 'Adolfo Builes'
},
relationships: {
messages: {
data: [
{ type: 'message', id: 'message-1' }
]
}
}
}]
});

user = env.store.peekRecord('user', 'user-1');

assert.equal(get(user, 'messages.firstObject.id'), 'message-1');
assert.equal(get(message, 'user.id'), 'user-1');
});
});