diff --git a/addon/adapters/application.js b/addon/adapters/application.js index 9330c8f..fc7c9cb 100644 --- a/addon/adapters/application.js +++ b/addon/adapters/application.js @@ -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); }, /** @@ -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)); }, /** @@ -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)); }, /** @@ -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)); }, /** diff --git a/addon/models/resource.js b/addon/models/resource.js index 534310d..43138bf 100644 --- a/addon/models/resource.js +++ b/addon/models/resource.js @@ -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; }); @@ -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));