Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more embedded association work to move from custom serializer to the …

…base. Still WIP
  • Loading branch information...
commit d1f7c01ef0b3010ff70bd3322c8e9461ce8af371 1 parent 542bf89
@mspisars mspisars authored
View
2  packages/ember-data/lib/system/associations/belongs_to.js
@@ -18,7 +18,7 @@ var hasAssociation = function(type, options, one) {
type = get(this, type, false) || get(window, type);
}
- id = options.embedded ? store.load(type, data[key]).id : data[key];
+ id = (options.embedded && data[key]) ? store.load(type, data[key]).id : data[key];
return id ? store.find(type, id) : null;
}).property('data').meta(meta);
};
View
2  packages/ember-data/lib/system/associations/has_many.js
@@ -16,7 +16,7 @@ var hasAssociation = function(type, options) {
type = get(this, type, false) || get(window, type);
}
- ids = options.embedded ? store.loadMany(type, data[key]).ids : data[key];
+ ids = (options.embedded && data[key]) ? store.loadMany(type, data[key]).ids : data[key];
association = store.findMany(type, ids || [], this, meta);
set(association, 'owner', this);
set(association, 'name', key);
View
44 packages/ember-data/lib/system/serializer.js
@@ -225,7 +225,7 @@ DS.Serializer = Ember.Object.extend({
this.addAttributes(hash, record);
- this.addRelationships(hash, record);
+ this.addRelationships(hash, record, options);
return hash;
},
@@ -249,20 +249,52 @@ DS.Serializer = Ember.Object.extend({
hash[primaryKey] = this.serializeId(id);
},
- addRelationships: function(hash, record) {
+ addRelationships: function(hash, record, options) {
record.eachAssociation(function(name, relationship) {
var key = this._keyForAttributeName(record.constructor, name);
if (relationship.kind === 'belongsTo') {
- this.addBelongsTo(hash, record, key, relationship);
+ this.addBelongsTo(hash, record, key, relationship, options);
} else if (relationship.kind === 'hasMany') {
- this.addHasMany(hash, record, key, relationship);
+ this.addHasMany(hash, record, key, relationship, options);
}
}, this);
},
- addBelongsTo: Ember.K,
- addHasMany: Ember.K,
+ addBelongsTo: function(hash, record, key, relationship, options) {
+ var value, id;
+
+ if (relationship.options.embedded) {
+ value = get(record, key);
+ hash[key] = value ? value.toJSON(options) : null;
+ } else {
+ id = record.get(key);
+ hash[key] = none(id) ? null : id;
+ }
+
+ },
+ addHasMany: function(hash, record, key, relationship, options) {
+ var manyArray = get(record, key),
+ records = [];
+
+ if (relationship.options.embedded) {
+ manyArray.forEach(function(record) {
+ records.push(record.toJSON(options));
+ });
+ } else {
+ var clientIds = get(manyArray, 'content'), id;
+
+ for (var i=0, l=clientIds.length; i<l; i++) {
+ id = get(record, 'store').clientIdToId[clientIds[i]];
+
+ if (id !== undefined) {
+ records.push(id);
+ }
+ }
+ }
+
+ hash[key] = records;
+ },
/**
Allows IDs to be normalized before being sent back to the
Please sign in to comment.
Something went wrong with that request. Please try again.