Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions addon/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,16 @@ export default Ember.Object.extend(FetchMixin, Evented, {
return RSVP.Promise.resolve(null);
}
json = json || { data: { id: resource.get('id'), type: resource.get('type') } };
json.data.relationships = relationships;
let cleanup = Ember.K;
if (relationships) {
json.data.relationships = relationships;
cleanup = resource._resetRelationships.bind(resource);
}
return this.fetch(url, {
method: 'PATCH',
body: JSON.stringify(json),
update: true
});
}).then(cleanup);
},

/**
Expand Down Expand Up @@ -248,7 +252,7 @@ export default Ember.Object.extend(FetchMixin, Evented, {
return this.fetch(this._urlForRelationship(resource, relationship), {
method: 'POST',
body: JSON.stringify(this.serializer.serializeRelationship(resource, relationship, id))
});
}).then(resource._resetRelationships.bind(resource));
},

/**
Expand Down Expand Up @@ -288,7 +292,7 @@ export default Ember.Object.extend(FetchMixin, Evented, {
return this.fetch(this._urlForRelationship(resource, relationship), {
method: 'PATCH',
body: JSON.stringify(this.serializer.serializeRelationship(resource, relationship))
});
}).then(resource._resetRelationships.bind(resource));
},

/**
Expand Down Expand Up @@ -318,7 +322,7 @@ export default Ember.Object.extend(FetchMixin, Evented, {
return this.fetch(this._urlForRelationship(resource, relationship), {
method: 'DELETE',
body: JSON.stringify(this.serializer.serializeRelationship(resource, relationship, id))
});
}).then(resource._resetRelationships.bind(resource));
},

/**
Expand Down
15 changes: 10 additions & 5 deletions addon/models/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ const Resource = Ember.Object.extend(ResourceOperationsMixin, {
let existing;
if (!Array.isArray(ids)) {
existing = this.get(relationshipData).id;
this.removeRelationship(relation, existing);
if (isType('string', ids)) {
this.addRelationship(relation, ids);
if (ids === null || isType('string', ids) && existing !== ids) {
this.removeRelationship(relation, existing);
if (ids !== null) {
this.addRelationship(relation, ids);
}
}
} else {
existing = this.get(relationshipData).map(function(rel) { return rel.id; });
Expand Down Expand Up @@ -245,9 +247,12 @@ const Resource = Ember.Object.extend(ResourceOperationsMixin, {
let meta = this.relationMetadata(relation);
setupRelationshipTracking.call(this, relation, meta.kind);
let ref = this._relationships[relation];
let relationshipData = this.get(`relationships.${relation}.data`);
if (meta && meta.kind === 'hasOne') {
ref.changed = identifier;
ref.previous = ref.previous || previous;
if (!relationshipData || relationshipData.id !== identifier.id) {
ref.changed = identifier;
ref.previous = ref.previous || previous;
}
} else if (meta && meta.kind === 'hasMany') {
let id = identifier.id;
ref.removals = Ember.A(ref.removals.rejectBy('id', id));
Expand Down