Skip to content
This repository has been archived by the owner on May 11, 2018. It is now read-only.

Deep serialization of nested-resource(s) #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/ember-rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Ember.Resource = Ember.Object.extend(Ember.ResourceAdapter, Ember.Copyable, {

REQUIRED: `resourceProperties` and `resourceName` (see note above)
*/
serialize: function() {
serialize: function(nested) {
var name = this.resourceName,
props = this.resourceProperties,
prop,
Expand All @@ -119,7 +119,11 @@ Ember.Resource = Ember.Object.extend(Ember.ResourceAdapter, Ember.Copyable, {
prop = props[i];
ret[name][prop] = this.serializeProperty(prop);
}
return ret;
if (nested || !name) {
return ret[name];
} else {
return ret;
}
},

/**
Expand All @@ -128,7 +132,24 @@ Ember.Resource = Ember.Object.extend(Ember.ResourceAdapter, Ember.Copyable, {
Override to provide custom serialization
*/
serializeProperty: function(prop) {
return this.get(prop);
var property = this.get(prop),
self = this;
if (Ember.Resource.detectInstance(property)) {
return this.serializeNestedResource(property);
} else if (Ember.isArray(property)) {
return property.map(function(item) {
return self.serializeNestedResource(item);
});
} else {
return property;
}
},

/**
Recursively generate nested resource's JSON representation
*/
serializeNestedResource: function(resource) {
return Ember.Resource.detectInstance(resource) ? resource.serialize(true) : resource;
},

/**
Expand Down