Skip to content

Commit

Permalink
[BUGFIX release] Guard against isDestroyed in ManyArray.flushCanonical
Browse files Browse the repository at this point in the history
Addresses emberjs#3084
  • Loading branch information
jgwhite committed Mar 4, 2016
1 parent 0534783 commit fc85604
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
5 changes: 4 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,9 @@ 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);
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');
});
});

0 comments on commit fc85604

Please sign in to comment.