Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Ember.Copyable mixin features if present #6

Merged
merged 2 commits into from
Oct 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions addon/mixins/copyable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const {
Logger,
guidFor,
isEmpty,
runInDebug
runInDebug,
canInvoke
} = Ember;

const {
Expand Down Expand Up @@ -137,12 +138,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 (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 {
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 {
Expand Down