From ba8f8a4f843d701679b0b896e9ef87c751511f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20PIRIO?= Date: Sat, 19 Aug 2017 12:43:37 +0200 Subject: [PATCH 1/2] Use Ember.Copyable mixin features if present --- addon/mixins/copyable.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/addon/mixins/copyable.js b/addon/mixins/copyable.js index 72d0ef1..618a4b7 100644 --- a/addon/mixins/copyable.js +++ b/addon/mixins/copyable.js @@ -137,12 +137,21 @@ export default Ember.Mixin.create({ !PRIMITIVE_TYPES.includes(type) ) { let value = this.get(name); - let transform = getTransform(this, type, _meta); - // Run the transform on the value. This should guarantee that we get - // a new instance. - value = transform.serialize(value, attributeOptions); - value = transform.deserialize(value, attributeOptions); + if ( + (Ember.typeOf(value) === 'instance') && + (Ember.typeOf(value.copy) === 'function') + ) { + value = value.copy(deep); + } + else { + let transform = getTransform(this, type, _meta); + + // Run the transform on the value. This should guarantee that we get + // a new instance. + value = transform.serialize(value, attributeOptions); + value = transform.deserialize(value, attributeOptions); + } attrs[name] = value; } else { From d46f91d5c25d27416afa1001534f74161acb3191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20PIRIO?= Date: Mon, 25 Sep 2017 08:55:36 +0200 Subject: [PATCH 2/2] Use Ember.canInvoke private method. --- addon/mixins/copyable.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/addon/mixins/copyable.js b/addon/mixins/copyable.js index 618a4b7..49cc18e 100644 --- a/addon/mixins/copyable.js +++ b/addon/mixins/copyable.js @@ -9,7 +9,8 @@ const { Logger, guidFor, isEmpty, - runInDebug + runInDebug, + canInvoke } = Ember; const { @@ -138,13 +139,13 @@ export default Ember.Mixin.create({ ) { let value = this.get(name); - if ( - (Ember.typeOf(value) === 'instance') && - (Ember.typeOf(value.copy) === 'function') - ) { + if (canInvoke(value, 'copy')) { + // "value" is an Ember.Object using the Ember.Copyable API (if you use + // the "Ember Data Model Fragments" addon and "value" is a fragment or + // if use your own serializer where you deserialize a value to an + // Ember.Object using this Ember.Copyable API) value = value.copy(deep); - } - else { + } else { let transform = getTransform(this, type, _meta); // Run the transform on the value. This should guarantee that we get