Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix: unload record with relationships #567

Merged
merged 1 commit into from

6 participants

@ppcano
  • calling clearRelationships before changing record state,
    otherwise 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

@ppcano ppcano 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
52fc905
@sly7-7
Collaborator

:+1: Great, I confirm this fix an issue I encounter.

@mspisars

I can confirm that this still works against master. :+1:
@wycats can we get this merged? Or is there something missing from this PR that needs to be added?

@bobey

I confirm that this PR fixes an issue on our app too.

@KasperTidemann

+1.

I too can confirm that this is a problem. Without this fix, calling record.unloadRecord() on a record that is in an appropriate state returns the following error:

Uncaught TypeError: Cannot call method 'relationshipBecameDirty' of undefined

@KasperTidemann

@tomdale, you got any input on this one?

@tomdale tomdale merged commit 6c2a961 into emberjs:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 31, 2012
  1. @ppcano

    Fix: unload record with relationships

    ppcano authored
    - 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
This page is out of date. Refresh to see the latest.
View
9 packages/ember-data/lib/system/model/states.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) {
View
50 packages/ember-data/tests/unit/store_test.js
@@ -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();
+
});
Something went wrong with that request. Please try again.