Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
svox1 committed Apr 13, 2015
2 parents 2b599ad + dcaf77b commit 19227fb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ var ActiveModelSerializer = RESTSerializer.extend({
type.eachRelationship(function(key, relationship) {
var payloadKey, payload;
if (relationship.options.polymorphic) {
payloadKey = this.keyForAttribute(key);
payloadKey = this.keyForAttribute(key, "deserialize");
payload = hash[payloadKey];
if (payload && payload.type) {
payload.type = this.typeForRoot(payload.type);
Expand All @@ -273,7 +273,7 @@ var ActiveModelSerializer = RESTSerializer.extend({
});
}
} else {
payloadKey = this.keyForRelationship(key, relationship.kind);
payloadKey = this.keyForRelationship(key, relationship.kind, "deserialize");
if (!hash.hasOwnProperty(payloadKey)) { return; }
payload = hash[payloadKey];
}
Expand Down
18 changes: 9 additions & 9 deletions packages/ember-data/lib/serializers/embedded-records-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
return extractEmbeddedRecords(this, this.store, type, normalizedHash);
},

keyForRelationship: function(key, type) {
if (this.hasDeserializeRecordsOption(key)) {
return this.keyForAttribute(key);
keyForRelationship: function(key, type, method) {
if ((method === 'serialize' && this.hasSerializeRecordsOption(key)) || (method === 'deserialize' && this.hasDeserializeRecordsOption(key))) {
return this.keyForAttribute(key, method);
} else {
return this._super(key, type) || key;
return this._super(key, type, method) || key;
}
},

Expand Down Expand Up @@ -192,14 +192,14 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
var embeddedSnapshot = snapshot.belongsTo(attr);
var key;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind);
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
if (!embeddedSnapshot) {
json[key] = null;
} else {
json[key] = embeddedSnapshot.id;
}
} else if (includeRecords) {
key = this.keyForAttribute(attr);
key = this.keyForAttribute(attr, 'serialize');
if (!embeddedSnapshot) {
json[key] = null;
} else {
Expand Down Expand Up @@ -300,10 +300,10 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
var includeRecords = this.hasSerializeRecordsOption(attr);
var key;
if (includeIds) {
key = this.keyForRelationship(attr, relationship.kind);
key = this.keyForRelationship(attr, relationship.kind, 'serialize');
json[key] = snapshot.hasMany(attr, { ids: true });
} else if (includeRecords) {
key = this.keyForAttribute(attr);
key = this.keyForAttribute(attr, 'serialize');
json[key] = snapshot.hasMany(attr).map(function(embeddedSnapshot) {
var embeddedJson = embeddedSnapshot.record.serialize({ includeId: true });
this.removeEmbeddedForeignKey(snapshot, embeddedSnapshot, relationship, embeddedJson);
Expand Down Expand Up @@ -336,7 +336,7 @@ var EmbeddedRecordsMixin = Ember.Mixin.create({
if (parentRecord) {
var name = parentRecord.name;
var embeddedSerializer = this.store.serializerFor(embeddedSnapshot.type);
var parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind);
var parentKey = embeddedSerializer.keyForRelationship(name, parentRecord.kind, 'deserialize');
if (parentKey) {
delete json[parentKey];
}
Expand Down
10 changes: 5 additions & 5 deletions packages/ember-data/lib/serializers/json-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default Serializer.extend({

if (this.keyForAttribute) {
type.eachAttribute(function(key) {
payloadKey = this.keyForAttribute(key);
payloadKey = this.keyForAttribute(key, 'deserialize');
if (key === payloadKey) { return; }
if (!hash.hasOwnProperty(payloadKey)) { return; }

Expand All @@ -220,7 +220,7 @@ export default Serializer.extend({

if (this.keyForRelationship) {
type.eachRelationship(function(key, relationship) {
payloadKey = this.keyForRelationship(key, relationship.kind);
payloadKey = this.keyForRelationship(key, relationship.kind, 'deserialize');
if (key === payloadKey) { return; }
if (!hash.hasOwnProperty(payloadKey)) { return; }

Expand Down Expand Up @@ -572,7 +572,7 @@ export default Serializer.extend({
var belongsTo = snapshot.belongsTo(key);
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key;
key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo", "serialize") : key;
json[key] = Ember.isNone(belongsTo) ? belongsTo : belongsTo.record.toJSON();
}
Expand All @@ -594,7 +594,7 @@ export default Serializer.extend({
// the serializer
var payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "belongsTo");
payloadKey = this.keyForRelationship(key, "belongsTo", "serialize");
}

//Need to check whether the id is there for new&async records
Expand Down Expand Up @@ -644,7 +644,7 @@ export default Serializer.extend({
// the serializer
payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany");
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
}

var relationshipType = snapshot.type.determineRelationshipType(relationship);
Expand Down
7 changes: 4 additions & 3 deletions packages/ember-data/lib/serializers/rest-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ function coerceId(id) {
```
You can also implement `keyForRelationship`, which takes the name
of the relationship as the first parameter, and the kind of
relationship (`hasMany` or `belongsTo`) as the second parameter.
of the relationship as the first parameter, the kind of
relationship (`hasMany` or `belongsTo`) as the second parameter, and
the method (`serialize` or `deserialize`) as the third parameter.
@class RESTSerializer
@namespace DS
Expand Down Expand Up @@ -739,7 +740,7 @@ var RESTSerializer = JSONSerializer.extend({
serializePolymorphicType: function(snapshot, json, relationship) {
var key = relationship.key;
var belongsTo = snapshot.belongsTo(key);
key = this.keyForAttribute ? this.keyForAttribute(key) : key;
key = this.keyForAttribute ? this.keyForAttribute(key, "serialize") : key;
if (Ember.isNone(belongsTo)) {
json[key + "Type"] = null;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,41 @@ test("serialize with embedded object (belongsTo relationship) supports serialize
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:id in conjunction with deserialize:records", function() {
env.registry.register('adapter:superVillain', DS.ActiveModelAdapter);
env.registry.register('serializer:superVillain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
secretLab: { serialize: 'id', deserialize: 'records' }
}
}));

var serializer = env.container.lookup("serializer:superVillain");

// records with an id, persisted
var tom, json;

run(function() {
tom = env.store.createRecord(
SuperVillain,
{ firstName: "Tom", lastName: "Dale", id: "1",
secretLab: env.store.createRecord(SecretLab, { minionCapacity: 5000, vicinity: "California, USA", id: "101" }),
homePlanet: env.store.createRecord(HomePlanet, { name: "Villain League", id: "123" })
}
);
});

run(function() {
json = serializer.serialize(tom._createSnapshot());
});

deepEqual(json, {
first_name: get(tom, "firstName"),
last_name: get(tom, "lastName"),
home_planet_id: get(tom, "homePlanet").get("id"),
secret_lab_id: get(tom, "secretLab").get("id")
});
});

test("serialize with embedded object (belongsTo relationship) supports serialize:false", function() {
env.registry.register('adapter:superVillain', DS.ActiveModelAdapter);
env.registry.register('serializer:superVillain', DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
Expand Down

0 comments on commit 19227fb

Please sign in to comment.