Skip to content

Commit 9bd182e

Browse files
mpiriooffirgolan
authored andcommitted
Use Ember.Copyable mixin features if present (#6)
* Use Ember.Copyable mixin features if present * Use Ember.canInvoke private method.
1 parent 89718b7 commit 9bd182e

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

addon/mixins/copyable.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const {
99
Logger,
1010
guidFor,
1111
isEmpty,
12-
runInDebug
12+
runInDebug,
13+
canInvoke
1314
} = Ember;
1415

1516
const {
@@ -137,12 +138,21 @@ export default Ember.Mixin.create({
137138
!PRIMITIVE_TYPES.includes(type)
138139
) {
139140
let value = this.get(name);
140-
let transform = getTransform(this, type, _meta);
141141

142-
// Run the transform on the value. This should guarantee that we get
143-
// a new instance.
144-
value = transform.serialize(value, attributeOptions);
145-
value = transform.deserialize(value, attributeOptions);
142+
if (canInvoke(value, 'copy')) {
143+
// "value" is an Ember.Object using the Ember.Copyable API (if you use
144+
// the "Ember Data Model Fragments" addon and "value" is a fragment or
145+
// if use your own serializer where you deserialize a value to an
146+
// Ember.Object using this Ember.Copyable API)
147+
value = value.copy(deep);
148+
} else {
149+
let transform = getTransform(this, type, _meta);
150+
151+
// Run the transform on the value. This should guarantee that we get
152+
// a new instance.
153+
value = transform.serialize(value, attributeOptions);
154+
value = transform.deserialize(value, attributeOptions);
155+
}
146156

147157
attrs[name] = value;
148158
} else {

0 commit comments

Comments
 (0)