From 6fcb440521069e50ee41ad748629854551869033 Mon Sep 17 00:00:00 2001 From: Sebastien Tisserant Date: Mon, 14 Sep 2015 13:31:59 +0200 Subject: [PATCH] Workaround for ED issue #3760 Temp workaround for ED issue https://github.com/emberjs/data/issues/3760 until the PR https://github.com/emberjs/data/pull/3765 will be merged. Also change class structure to fit the current one (with a dedicated `_shouldSerializeHasMany` method). --- addon/serializers/localforage.js | 44 ++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/addon/serializers/localforage.js b/addon/serializers/localforage.js index 838594a..b6d6df0 100644 --- a/addon/serializers/localforage.js +++ b/addon/serializers/localforage.js @@ -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 } } @@ -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;