Skip to content

Commit 18bdc6a

Browse files
authored
Cleanup relationships change tracking (#107)
1 parent f000fd0 commit 18bdc6a

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

addon/adapters/application.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,16 @@ export default Ember.Object.extend(FetchMixin, Evented, {
196196
return RSVP.Promise.resolve(null);
197197
}
198198
json = json || { data: { id: resource.get('id'), type: resource.get('type') } };
199-
json.data.relationships = relationships;
199+
let cleanup = Ember.K;
200+
if (relationships) {
201+
json.data.relationships = relationships;
202+
cleanup = resource._resetRelationships.bind(resource);
203+
}
200204
return this.fetch(url, {
201205
method: 'PATCH',
202206
body: JSON.stringify(json),
203207
update: true
204-
});
208+
}).then(cleanup);
205209
},
206210

207211
/**
@@ -248,7 +252,7 @@ export default Ember.Object.extend(FetchMixin, Evented, {
248252
return this.fetch(this._urlForRelationship(resource, relationship), {
249253
method: 'POST',
250254
body: JSON.stringify(this.serializer.serializeRelationship(resource, relationship, id))
251-
});
255+
}).then(resource._resetRelationships.bind(resource));
252256
},
253257

254258
/**
@@ -288,7 +292,7 @@ export default Ember.Object.extend(FetchMixin, Evented, {
288292
return this.fetch(this._urlForRelationship(resource, relationship), {
289293
method: 'PATCH',
290294
body: JSON.stringify(this.serializer.serializeRelationship(resource, relationship))
291-
});
295+
}).then(resource._resetRelationships.bind(resource));
292296
},
293297

294298
/**
@@ -318,7 +322,7 @@ export default Ember.Object.extend(FetchMixin, Evented, {
318322
return this.fetch(this._urlForRelationship(resource, relationship), {
319323
method: 'DELETE',
320324
body: JSON.stringify(this.serializer.serializeRelationship(resource, relationship, id))
321-
});
325+
}).then(resource._resetRelationships.bind(resource));
322326
},
323327

324328
/**

addon/models/resource.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,11 @@ const Resource = Ember.Object.extend(ResourceOperationsMixin, {
141141
let existing;
142142
if (!Array.isArray(ids)) {
143143
existing = this.get(relationshipData).id;
144-
this.removeRelationship(relation, existing);
145-
if (isType('string', ids)) {
146-
this.addRelationship(relation, ids);
144+
if (ids === null || isType('string', ids) && existing !== ids) {
145+
this.removeRelationship(relation, existing);
146+
if (ids !== null) {
147+
this.addRelationship(relation, ids);
148+
}
147149
}
148150
} else {
149151
existing = this.get(relationshipData).map(function(rel) { return rel.id; });
@@ -245,9 +247,12 @@ const Resource = Ember.Object.extend(ResourceOperationsMixin, {
245247
let meta = this.relationMetadata(relation);
246248
setupRelationshipTracking.call(this, relation, meta.kind);
247249
let ref = this._relationships[relation];
250+
let relationshipData = this.get(`relationships.${relation}.data`);
248251
if (meta && meta.kind === 'hasOne') {
249-
ref.changed = identifier;
250-
ref.previous = ref.previous || previous;
252+
if (!relationshipData || relationshipData.id !== identifier.id) {
253+
ref.changed = identifier;
254+
ref.previous = ref.previous || previous;
255+
}
251256
} else if (meta && meta.kind === 'hasMany') {
252257
let id = identifier.id;
253258
ref.removals = Ember.A(ref.removals.rejectBy('id', id));

0 commit comments

Comments
 (0)