Permalink
Browse files

fix identity mapping in hasMany

  • Loading branch information...
1 parent 7fb68d3 commit bb533217d76046d2f671a53d2e2aea790f786023 @jamesmacaulay jamesmacaulay committed Jun 21, 2012
View
2 lib/batman.js
@@ -9990,7 +9990,7 @@
record._withoutDirtyTracking(function() {
return this.fromJSON(jsonObject);
});
- existingRecord = relatedModel.get('loaded').indexedByUnique(association.foreignKey).get(record.get('id'));
+ existingRecord = relatedModel.get('loaded').indexedByUnique('id').get(record.get('id'));
if (existingRecord != null) {
existingRecord._withoutDirtyTracking(function() {
return this.fromJSON(jsonObject);
View
2 lib/dist/batman.node.js
@@ -9990,7 +9990,7 @@
record._withoutDirtyTracking(function() {
return this.fromJSON(jsonObject);
});
- existingRecord = relatedModel.get('loaded').indexedByUnique(association.foreignKey).get(record.get('id'));
+ existingRecord = relatedModel.get('loaded').indexedByUnique('id').get(record.get('id'));
if (existingRecord != null) {
existingRecord._withoutDirtyTracking(function() {
return this.fromJSON(jsonObject);
View
2 src/model/associations/has_many_association.coffee
@@ -42,7 +42,7 @@ class Batman.HasManyAssociation extends Batman.PluralAssociation
for jsonObject in data
record = new relatedModel()
record._withoutDirtyTracking -> @fromJSON(jsonObject)
- existingRecord = relatedModel.get('loaded').indexedByUnique(association.foreignKey).get(record.get('id'))
+ existingRecord = relatedModel.get('loaded').indexedByUnique('id').get(record.get('id'))
if existingRecord?
existingRecord._withoutDirtyTracking -> @fromJSON jsonObject
record = existingRecord
View
46 tests/batman/model/associations/has_many_test.coffee
@@ -25,10 +25,28 @@ QUnit.module "Batman.Model hasMany Associations"
name: "Product One"
id: 1
store_id: 1
+ productVariants: [{
+ id:3
+ price:50
+ product_id:1
+ },{
+ id:4
+ price:60
+ product_id:1
+ }]
products2:
name: "Product Two"
id: 2
store_id: 1
+ productVariants: [{
+ id:1
+ price:50
+ product_id:2
+ },{
+ id:2
+ price:60
+ product_id:2
+ }]
products3:
name: "Product Three"
id: 3
@@ -48,6 +66,22 @@ QUnit.module "Batman.Model hasMany Associations"
@belongsTo 'product', namespace: namespace
@variantsAdapter = createStorageAdapter @ProductVariant, AsyncTestStorageAdapter,
+ product_variants1:
+ id:1
+ price:50
+ product_id:2
+ product_variants2:
+ id:2
+ price:60
+ product_id:2
+ product_variants3:
+ id:3
+ price:50
+ product_id:1
+ product_variants4:
+ id:4
+ price:60
+ product_id:1
product_variants5:
id:5
price:50
@@ -354,7 +388,8 @@ asyncTest "unsaved hasMany models should decode their child records based on ID"
asyncTest "unsaved hasMany models should decode their existing child records based on ID", ->
@ProductVariant.load (err, variants) =>
product = new @Product
- product.get('productVariants').add(variant) for variant in variants
+ for variant in variants when variant.get('id') in [5,6]
+ product.get('productVariants').add(variant)
five = product.get('productVariants').indexedByUnique('id').get(5)
six = product.get('productVariants').indexedByUnique('id').get(6)
@@ -484,6 +519,15 @@ asyncTest "hasMany supports custom foreign keys", 1, ->
delay ->
equal products.length, 3
+asyncTest "regression test: identity mapping works", ->
+ @ProductVariant.load (err, variants) =>
+ originalIDs = variants.map (v) -> v.get('id')
+ @Product.load (err, products) =>
+ currentIDs = variants.map (v) -> v.get('id')
+ deepEqual currentIDs, originalIDs
+ deepEqual @ProductVariant.get('loaded').mapToProperty('id').sort(), [1,2,3,4,5,6]
+ QUnit.start()
+
QUnit.module "Batman.Model hasMany Associations with inverse of"
setup: ->
namespace = {}

1 comment on commit bb53321

@airhorns

Niiiicceeee guy

Please sign in to comment.