Skip to content

Commit

Permalink
Workaround for ED issue #3760
Browse files Browse the repository at this point in the history
Temp workaround for ED issue emberjs/data#3760 until
PR emberjs/data#3765 is merged.

Change class structure also to reflect the new one introduced earlier by ED 1.13
(with dedicated `_shouldSerializeHasMany`).
  • Loading branch information
sebweaver committed Sep 14, 2015
1 parent 4b10199 commit 6daca13
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions addon/serializers/localforage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,35 @@ export default DS.JSONSerializer.extend({

isNewSerializerAPI: true,

serializeHasMany: function (snapshot, json, relationship) {
var key = relationship.key;

if (this._canSerialize(key)) {
var payloadKey;
_shouldSerializeHasMany: function (snapshot, key, relationship) {
var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store);
if (this._mustSerialize(key)) {
return true;
}
return this._canSerialize(key) &&
(relationshipType === 'manyToNone' ||
relationshipType === 'manyToMany' ||
relationshipType === 'manyToOne');
},

// if provided, use the mapping provided by `attrs` in
// the serializer
payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
}
// Omit the unknown hasMany relationships of pushed record
// (see https://github.com/emberjs/data/issues/3760)
// TODO: this override will be unecessary after merge of the following PR:
// https://github.com/emberjs/data/pull/3765
serializeHasMany: function(snapshot, json, relationship) {
var key = relationship.key;

var relationshipType = snapshot.type.determineRelationshipType(relationship, this.store);
if (this._shouldSerializeHasMany(snapshot, key, relationship)) {
var hasMany = snapshot.hasMany(key, { ids: true });
if (hasMany !== undefined) {
// if provided, use the mapping provided by `attrs` in
// the serializer
var payloadKey = this._getMappedKey(key);
if (payloadKey === key && this.keyForRelationship) {
payloadKey = this.keyForRelationship(key, "hasMany", "serialize");
}

if (relationshipType === 'manyToNone' ||
relationshipType === 'manyToMany' ||
relationshipType === 'manyToOne') {
json[payloadKey] = snapshot.hasMany(key, {ids: true});
json[payloadKey] = hasMany;
// TODO support for polymorphic manyToNone and manyToMany relationships
}
}
Expand Down Expand Up @@ -91,6 +101,8 @@ export default DS.JSONSerializer.extend({

// Remove the undefined hasMany relationships which will fail at normalization
// (see https://github.com/emberjs/data/issues/3736)
// TODO: this block will be unecessary after merge of the following PR:
// https://github.com/emberjs/data/pull/3747
var relationshipNames = Ember.get(primaryModelClass, 'relationshipNames');
var relationships = relationshipNames.hasMany;

Expand Down

0 comments on commit 6daca13

Please sign in to comment.