Skip to content

Commit

Permalink
store#unloadRecord to destroy record. add test to show record is
Browse files Browse the repository at this point in the history
destroyed and observers removed
  • Loading branch information
ryanpatrickcook committed May 24, 2016
1 parent 2001ee3 commit 9ab9c07
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
4 changes: 2 additions & 2 deletions addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ var Model = Ember.Object.extend(Ember.Evented, {
@type {Boolean}
@readOnly
*/
hasDirtyAttributes: Ember.computed('currentState.isDirty', function() {
return this.get('currentState.isDirty');
hasDirtyAttributes: Ember.computed('currentState', function() {
return this.get('_internalModel.currentState.isDirty');
}),
/**
If this property is `true` the record is in the `saving` state. A
Expand Down
4 changes: 2 additions & 2 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ Store = Service.extend({
*/
unloadRecord(record) {
record.unloadRecord();
record.destroy();
},

// ................
Expand Down Expand Up @@ -1360,8 +1361,7 @@ Store = Service.extend({

for (let i = 0; i < records.length; i++) {
record = records[i];
record.unloadRecord();
record.destroy(); // maybe within unloadRecord
this.unloadRecord(record);
}

typeMap.metadata = new EmptyObject();
Expand Down
39 changes: 38 additions & 1 deletion tests/unit/store/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ testInDebug("unload a dirty record asserts", function(assert) {
});

test("unload a record", function(assert) {
assert.expect(5);
assert.expect(6);

run(function() {
store.push({
Expand All @@ -87,6 +87,7 @@ test("unload a record", function(assert) {
});

assert.equal(get(record, 'hasDirtyAttributes'), false, "record is not dirty");
assert.equal(get(record, 'isDestroyed'), true, 'record is destroyed');
assert.equal(get(record, 'isDeleted'), true, "record is deleted");

tryToFind = false;
Expand All @@ -97,6 +98,42 @@ test("unload a record", function(assert) {
});
});

test("unload a record - observers should be removed", function(assert) {
assert.expect(4);

run(function() {
store.push({
data: {
type: 'record',
id: '1',
attributes: {
title: 'toto'
}
}
});

store.findRecord('record', 1).then(function(record) {
Ember.addObserver(record, 'title', record, function() {
assert.ok(false, 'observer for record.title');
});

let observers = Ember.observersFor(record, 'title');
assert.equal(observers.length, 1, 'record should have 1 observer');

run(function() {
store.unloadRecord(record);
});

run(function() {
let observers = Ember.observersFor(record, 'title');
assert.equal(get(record, 'isDeleted'), true, 'record is deleted');
assert.equal(get(record, 'isDestroyed'), true, 'record is destroyed');
assert.equal(observers.length, 0, 'record should not have observers');
});
});
});
});

module("DS.Store - unload record with relationships");


Expand Down

0 comments on commit 9ab9c07

Please sign in to comment.