Permalink
Browse files

Fix: unload record with relationships

- calling `clearRelationships` before changing record state,
  otherwise it fails, because properties try to update removed
  relationships

- Transaction commit failed when an unloaded record was in the
  `transaction.bucketForType.updated` array. Using recordIsMoving fixed
  this issue
  • Loading branch information...
1 parent 2ac66cd commit 52fc905288b91d877c9a75d8f7cf93b7d91c176d @ppcano committed Dec 31, 2012
Showing with 58 additions and 1 deletion.
  1. +8 −1 packages/ember-data/lib/system/model/states.js
  2. +50 −0 packages/ember-data/tests/unit/store_test.js
@@ -572,8 +572,15 @@ var states = {
},
unloadRecord: function(manager) {
+ var record = get(manager, 'record');
+
+ // clear relationships before moving to deleted state
+ // otherwise it fails
+ record.clearRelationships();
+ record.withTransaction(function(t) {
+ t.recordIsMoving('updated', record);
+ });
manager.transitionTo('deleted.saved');
- get(manager, 'record').clearRelationships();
},
willCommit: function(manager) {
@@ -726,4 +726,54 @@ test("unload a record", function() {
tryToFind = false;
store.find(Record, 1);
equal(tryToFind, true, "not found record with id 1");
+
+});
+
+module("DS.Store - unload record with relationships");
+
+test("can commit store after unload record with relationships", function() {
+
+ var store = DS.Store.create({
+ adapter: DS.Adapter.create({
+
+ find: function() {
+ tryToFind = true;
+ },
+ createRecord: function(store, type, record) {
+ this.didCreateRecord(store, type, record);
+ }
+ })
+ });
+ var like, product, brand;
+
+ var Brand = DS.Model.extend({
+ name: DS.attr('string')
+ });
+ var Product = DS.Model.extend({
+ description: DS.attr('string'),
+ brand: DS.belongsTo(Brand)
+ });
+ var Like = DS.Model.extend({
+ product: DS.belongsTo(Product)
+ });
+
+ store.load(Brand, {id: 1, name: 'EmberJS'});
+ brand = store.find(Brand, 1);
+
+ store.load(Product, {id: 1, description: 'toto', brand: brand});
+ product = store.find(Product, 1);
+
+ like = store.createRecord(Like, {id: 1, product: product});
+ store.commit();
+
+ store.unloadRecord(product);
+ // can commit because `product` is not in transactionBucketTypes
+ store.commit();
+
+ tryToFind = false;
+ product = store.find(Product, 1);
+ ok(tryToFind, "not found record with id 1");
+
+ store.destroy();
+
});

0 comments on commit 52fc905

Please sign in to comment.