Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Only serialize has-manys that are embedded

  • Loading branch information...
commit 6c2346dce50cbdee64a95b1d27d14d54adaa97fe 1 parent e99cb9f
Tom Dale authored
View
42 packages/ember-data/lib/serializers/json_serializer.js
@@ -109,22 +109,42 @@ DS.JSONSerializer = DS.Serializer.extend({
}
},
+ /**
+ Adds a has-many relationship to the JSON hash being built.
+
+ The default REST semantics are to only add a has-many relationship if it
+ is embedded. If the relationship was initially loaded by ID, we assume that
+ that was done as a performance optimization, and that changes to the
+ has-many should be saved as foreign key changes on the child's belongs-to
+ relationship.
+
+ @param {Object} hash the JSON being built
+ @param {DS.Model} record the record being serialized
+ @param {String} key the JSON key into which the serialized relationship
+ should be saved
+ @param {Object} relationship metadata about the relationship being serialized
+ */
addHasMany: function(hash, record, key, relationship) {
var type = record.constructor,
name = relationship.key,
- array = [],
- self = this,
- target;
+ serializedHasMany = [],
+ manyArray, embeddedType;
- if (this.embeddedType(type, name)) {
- if (target = get(record, name)) {
- target.forEach(function (record) {
- array.push(self.serialize(record, { includeId: true }));
- });
- }
- }
+ // If the has-many is not embedded, there is nothing to do.
+ embeddedType = this.embeddedType(type, name);
+ if (embeddedType !== 'always') { return; }
+
+ // Get the DS.ManyArray for the relationship off the record
+ manyArray = get(record, name);
+
+ // Build up the array of serialized records
+ manyArray.forEach(function (record) {
+ serializedHasMany.push(this.serialize(record, { includeId: true }));
+ }, this);
- hash[key] = array;
+ // Set the appropriate property of the serialized JSON to the
+ // array of serialized embedded records
+ hash[key] = serializedHasMany;
},
// EXTRACTION
View
17 packages/ember-data/tests/integration/embedded/embedded_without_ids_test.js
@@ -1,5 +1,5 @@
var store, Adapter, adapter;
-var Post, Comment, User, Pingback;
+var Post, Comment, User, Pingback, Like;
var attr = DS.attr;
module("Embedded Relationships Without IDs", {
@@ -15,13 +15,15 @@ module("Embedded Relationships Without IDs", {
user: DS.belongsTo(User)
});
- Pingback = App.Pingback = DS.Model.extend({
- });
+ Pingback = App.Pingback = DS.Model.extend();
+
+ Like = App.Like = DS.Model.extend();
Post = App.Post = DS.Model.extend({
title: attr('string'),
comments: DS.hasMany(Comment),
- pingbacks: DS.hasMany(Pingback)
+ pingbacks: DS.hasMany(Pingback),
+ likes: DS.hasMany(Like)
});
Adapter = DS.RESTAdapter.extend();
@@ -32,7 +34,8 @@ module("Embedded Relationships Without IDs", {
Adapter.map(Post, {
comments: { embedded: 'always' },
- pingbacks: { embedded: 'always' }
+ pingbacks: { embedded: 'always' },
+ likes: { embedded: 'load' }
});
adapter = Adapter.create();
@@ -157,7 +160,9 @@ asyncTest("Embedded hasMany relationships can be saved when embedded: always is
},
{
title: "This does not seem to reflect the Unix philosophy haha"
- }]
+ }],
+
+ likes: [{ id: 1 }, { id: 2 }]
});
adapter.ajax = function(url, type, hash) {
Please sign in to comment.
Something went wrong with that request. Please try again.