JsonSerializer's serialization for embedded has-many relationships includes too many records #576

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+26 −9
Diff settings

Always

Just for now

@@ -110,11 +110,19 @@ DS.JSONSerializer = DS.Serializer.extend({
},
addHasMany: function(hash, record, key, relationship) {
- var array = [];
+ var type = record.constructor,
+ name = relationship.key,
+ array = [],
+ self = this,
+ target;
- this.eachEmbeddedHasManyRecord(record, function(embeddedRecord) {
- array.push(this.serialize(embeddedRecord, { includeId: true }));
- }, this);
+ if (this.embeddedType(type, name)) {
+ if (target = get(record, name)) {
+ target.forEach(function (record) {
+ array.push(self.serialize(record, { includeId: true }));
+ });
+ }
+ }
hash[key] = array;
},
@@ -1,5 +1,5 @@
var store, Adapter, adapter;
-var Post, Comment, User;
+var Post, Comment, User, Pingback;
var attr = DS.attr;
module("Embedded Relationships Without IDs", {
@@ -15,9 +15,13 @@ module("Embedded Relationships Without IDs", {
user: DS.belongsTo(User)
});
+ Pingback = App.Pingback = DS.Model.extend({
+ });
+
Post = App.Post = DS.Model.extend({
title: attr('string'),
- comments: DS.hasMany(Comment)
+ comments: DS.hasMany(Comment),
+ pingbacks: DS.hasMany(Pingback)
});
Adapter = DS.RESTAdapter.extend();
@@ -27,7 +31,8 @@ module("Embedded Relationships Without IDs", {
});
Adapter.map(Post, {
- comments: { embedded: 'always' }
+ comments: { embedded: 'always' },
+ pingbacks: { embedded: 'always' }
});
adapter = Adapter.create();
@@ -167,7 +172,9 @@ asyncTest("Embedded hasMany relationships can be saved when embedded: always is
{
title: "This does not seem to reflect the Unix philosophy haha",
user: null
- }]
+ }],
+
+ pingbacks: []
}
});
@@ -265,7 +272,9 @@ asyncTest("Embedded records that contain embedded records can be saved", functio
user: {
name: "microuser"
}
- }]
+ }],
+
+ pingbacks: []
}
});