Skip to content

Commit 20a2df8

Browse files
author
Lee Richmond
committed
Support async: false relationships
1 parent 7682f43 commit 20a2df8

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

addon/mixins/nested-relations.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ const iterateRelations = function(record, relations, callback) {
1111
Object.keys(relations).forEach((relationName) => {
1212
let subRelations = relations[relationName];
1313

14-
let kind = record.relationshipFor(relationName).kind;
14+
let metadata = record.relationshipFor(relationName);
15+
let kind = metadata.kind;
1516
let relatedRecord = record.get(relationName);
16-
relatedRecord = relatedRecord.get('content');
17+
18+
if (metadata.options.async !== false) {
19+
relatedRecord = relatedRecord.get('content');
20+
}
1721

1822
if (relatedRecord) {
1923
callback(relationName, kind, relatedRecord, subRelations);
@@ -158,6 +162,16 @@ export default Ember.Mixin.create({
158162
delete(json.data.attributes);
159163
}
160164

165+
if (adapterOptions.attributes) {
166+
if (!json.data.attributes) {
167+
json.data.attributes = {};
168+
}
169+
170+
Object.keys(adapterOptions.attributes).forEach((k) => {
171+
json.data.attributes[k] = adapterOptions.attributes[k];
172+
});
173+
}
174+
161175
let relationships = relationshipsDirective(adapterOptions.relationships);
162176
processRelationships(relationships, json.data, snapshot.record);
163177
snapshot.record.set('__recordsJustSaved', savedRecords);

tests/unit/mixins/nested-relations-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const Post = DS.Model.extend(ModelMixin, NestedRelationsMixin, {
3636
title: DS.attr('string'),
3737
genre: DS.belongsTo(),
3838
author: DS.belongsTo(),
39+
asyncFalseAuthor: DS.belongsTo('author', { async: false }),
3940
tags: DS.hasMany()
4041
});
4142

@@ -208,6 +209,29 @@ test('it serializes one-to-one correctly', function(assert) {
208209
assert.deepEqual(json, expectedJSON, 'has correct json');
209210
});
210211

212+
test('it serializes async: false relationships correctly', function(assert) {
213+
let post = store.createRecord('post', {
214+
asyncFalseAuthor: store.createRecord('author', { name: 'Joe Author' })
215+
});
216+
let json = serialize(post, { attributes: false, relationships: 'asyncFalseAuthor' });
217+
let expectedJSON = {
218+
data: {
219+
type: 'posts',
220+
relationships: {
221+
'async-false-author': {
222+
data: {
223+
type: 'authors',
224+
attributes: {
225+
name: 'Joe Author'
226+
}
227+
}
228+
}
229+
}
230+
}
231+
};
232+
assert.deepEqual(json, expectedJSON, 'has correct json');
233+
});
234+
211235
test('it serializes has one marked for deletion correctly', function(assert) {
212236
seedPostWithAuthor();
213237

0 commit comments

Comments
 (0)